Can't connect to Mongo Atlas

Written by Arvind

I’m gonna make this short. I was getting like this.

UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 30000 ms at new MongooseTimeoutError

Then at the last, it seems I needed to add the .catch function.

Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

So I change my connection from

var mongoose = require('mongoose');
mongoose.connect(`${process.env.MONGOOSE}`, { useNewUrlParser: true, useUnifiedTopology: true });

To this

mongoose.connect(
  `${process.env.MONGOOSE}`, { useNewUrlParser: true })
  .then(() => console.log('Connected'))
  .catch(err => console.log('Caught **** bon ****', err.stack)
);

And try to run the server again, but I get another error,

Caught **** bon**** MongoNetworkError: failed to connect to server [sobar-shard-00-00-p3hkj.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to sobar-shard-00-00-p3hkj.mongodb.net:27017 closed]

So I googled and stacking overflowings search for the answer… All ever need to do, is to Add Whitelist Entry in my Mongo Atlas project! And ALLOW ACCESS FROM ANYWHERE.

I hope this one helps you. Chow!