I hope someone could help me because I confused with this problem I thought my code is OK but Atlas seems reject so I couldn't connect to Atlas. The connection so simple. I fill those params from nodemon.json:
const {
MONGODB_ATLAS_USERNAME,
MONGODB_ATLAS_PASSWORD,
MONGODB_ATLAS_DBNAME
} = process.env
var mongoose = require('mongoose');
app.listen(PORT, () => {
console.info(`App is running! at http://localhost:${PORT}`)
})
mongoose.connect(`mongodb://${MONGODB_ATLAS_USERNAME}:${MONGODB_ATLAS_PASSWORD}@cluster0.lzjty.mongodb.net/${MONGODB_ATLAS_DBNAME}?retryWrites=true&w=majority&ssl=true`,{useNewUrlParser:true,useUnifiedTopology:true});
On Atlas, I whitelisted my IP and allow all IP to connect
But every npm run, the return always:
App is running! at http://localhost:8080
[1] (node:2012) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: 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. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
It generally happens when you use public IP. It changes every time you connect to the internet and you need to make changes to the MONGO Atlas. I would suggest you make these changes for development.
Follow this:- MongoDB Atlas -> NetworkAccess -> Edit -> Allow Any