Reputation: 33
i'm successfully connecting my mongodb to a cluster in the mongoATlas but when i try to connect to heroku i get this error :
> UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection <monitor> to 52.55.178.46:27017 closed
2020-03-29T22:14:32.041176+00:00 app[web.1]: at new MongooseServerSelectionError (/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
2020-03-29T22:14:32.041176+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:823:32)
2020-03-29T22:14:32.041178+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:333:15)
2020-03-29T22:14:32.041178+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:10:4)
and this is my code for connecting to mongoDb:
const uri =
"mongodb+srv://theosadxen:[email protected]/auth";
mongoose
.connect(uri, {
useUnifiedTopology: true,
useCreateIndex: true,
useNewUrlParser: true
})
.then(() => console.log("Connected to mongodb..."));
Upvotes: 0
Views: 614
Reputation: 11
I also faced a similar problem, and the first thing to do is test the new connection* (mongodb+srv://theosadxen:[email protected]/auth), still running from your computer. If the connection is established, then try:
Make sure your IP is whitelisted. The easiest solution is add the "All IPs" whitelist of 0.0.0.0/0
Then change the user's password to the password suggested by mongodb (Autogenerate Secure Password)
Modify this in your application and make a new commit for heroku, to restart everything. It worked for me, and I hope it helps you too.
*the end of URI is strange! Did you change something on this string?
Upvotes: 1