'How can I solve this problem with mongoose connection?
This code worked for a while and I don't know what I did but it gives me this error. I saw a some guys said that update the Node, but it doesn't work for me. Mine is latest one.
This is error message
mongodb connection failed
TypeError:[objectOptions,urlOptions,exports.DEFAULT_OPTIONS].flatMap is not a function
at parseOptions(/root/app/node_modules/mongodb/lib/connection_string.js:258:77)
at new MongoClient (/root/app/node_modules/mongodb/lib/mongo_client.js:62:63)
at Promise (/root/app/node_modules/mongoose/lib/connection.js:784:16)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/root/app/node_modules/mongoose/lib/connection.js:781:19)
at _mongoose._promiseOrCallback.cb (/root/app/node_modules/mongoose/lib/index.js:342:10)
at Promise (/root/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5)
at new Promise (<anonymous>)
at promiseOrCallback (/root/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/root/app/node_modules/mongoose/lib/index.js:1181:10)
and this is my connection code //mongoDB connection
const connect = () => {
if(process.env.NODE_ENV !== 'production'){
mongoose.set('debug', true);
}
mongoose.connect('mongodb+srv://<name>:<pw>@cluster0.ukhax.mongodb.net/Users?retryWrites=true&w=majority',(error) => {
if(error){console.log("mongodb connection failed");
console.log(error);}
else{console.log("User mongodb connected");}
});
};
connect();
Solution 1:[1]
Mongoose from time to time adds breaking changes. mongoos.connect
has become an async method that returns a promise, so you need to change your code from callback based to promise based:
const connect = () => {
if(process.env.NODE_ENV !== 'production'){
mongoose.set('debug', true);
}
mongoose.connect('mongodb+srv://<name>:<pw>@cluster0.ukhax.mongodb.net/Users?retryWrites=true&w=majority')
.then(() => {
console.log("User mongodb connected");
connect()
})
.catch(e => console.log(e))
Solution 2:[2]
Had the same error. Worked for me after downgrading mongoose to "mongoose": "^6.2.8". Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cesare Polonara |
Solution 2 | Daniel Maina |