Reputation: 161
I am trying to connect to mongoDB, this is the code that I am using:
const mongoose = require('mongoose');
const db = 'mongodb://<username>:<password>@xxxx-uaglu.mongodb.net/<dbname>?retryWrites=true&w=majority'
const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log('MongoDB connected...');
} catch (err) {
console.error(err.message);
process.exit(1);
}
}
module.exports = connectDB;
In security added 0.0.0.0/0 to IP whitelist.
I keep getting the following error message: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted.
Does anyone know what I can do? This is just a personal project but it's frustrating that I can't move ahead with it.
Upvotes: 0
Views: 155
Reputation: 19
in your database URI You got to replace with username that you created on mongodb Atlas same goes with the and . For example if your credentials are:- username: jonDoe, password: test@abcd, database name: myBlogs
mongodb://jonDoe:test@[email protected]/myBlogs?retryWrites=true&w=majority.
Upvotes: 0
Reputation: 11
I have solved your same problem changing the user database password with a password like name.number in the database access in the security section. Then copying that password in the MongoURI
Upvotes: 1