I am using Mongoose with my Node.js app and this is my configuration:
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopol
I was also facing the same issue:
I made sure to be connected to mongoDB by running the following on the terminal:
brew services start mongodb-community@4.2
And I got the output:
Successfully started `mongodb-community`
Instructions for installing mongodb at
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
or https://www.youtube.com/watch?v=IGIcrMTtjoU
My configuration was as follows:
mongoose.connect(config.mongo_uri, {
useUnifiedTopology: true,
useNewUrlParser: true})
.then(() => console.log("Connected to Database"))
.catch(err => console.error("An error has occured", err));
Which solved my problem!
I want to add to this thread that it may also have to do with other dependencies.
For instance, nothing I updated or set for NodeJS, MongoDB or Mongoose were the issue - however - connect-mongodb-session
had been updated and starting slinging the same error. The solution, in this case, was to simply rollback the version of connect-mongodb-session
from version 2.3.0
to 2.2.0
.
Setting mongoose connect useUnifiedTopology: true option
import mongoose from 'mongoose';
const server = '127.0.0.1:27017'; // REPLACE WITH YOUR DB SERVER
const database = 'DBName'; // REPLACE WITH YOUR DB NAME
class Database {
constructor() {
this._connect();
}
_connect() {
mongoose.Promise = global.Promise;
// * Local DB SERVER *
mongoose
.connect(`mongodb://${server}/${database}`, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(
() => console.log(`mongoose version: ${mongoose.version}`),
console.log('Database connection successful'),
)
.catch(err => console.error('Database connection error', err));
}
}
module.exports = new Database();
You Can Try async await
const connectDB = async () => {
try {
await mongoose.connect(<database url>, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
console.log("MongoDB Conected")
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
mongoose.connect("DBURL", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true },(err)=>{
if(!err){
console.log('MongoDB connection sucess');
}
else{
console.log('connection not established :' + JSON.stringify(err,undefined,2));
}
});