question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled Promise Rejection Warning: Mongoose Server Selection Error

See original GitHub issue

UnhandledPromiseRejectionWarning: MongooseServerSelectionError Bug?*

What is the current behavior?

  • hello there I am trying to connect mongoose with URL string included everything is fine but after console.log(“mongoDB database connection established successfully”) this is not showing this console’s text but an error is coming there Screenshot from 2021-02-03 14-54-33
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;
mongoose.connect(uri,{useNewUrlParser:true,useCreateIndex:true,useUnifiedTopology:true} 
    );
const connection = mongoose.connection;
connection.once('open',() =>{
console.log("mongoDB database connection established successfully");
})
app.listen(port, () => {
  console.log(`server is running on port ${port}`);
});

server.js file code above and in .env the URI key is given

What is the expected behavior?

  • the expected output is MongoDB database connection established successfully.

What are the versions of Node.js, Mongoose, and MongoDB you are using? Note that “latest” is not a version.

  • “cors”: “^2.8.5”, “dotenv”: “^8.2.0”, “express”: “^4.17.1”, “mongoose”: “^5.11.14”,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
deistermatheuscommented, Feb 4, 2021

@krishnadevz , a few things that might help people troubleshoot your issue:

1 - Does the shell connect successfully from your system or is the issue specific to the node app? 2 - Can you post the connection string format you are using in your env config? ex: the connection string format that you have copied and pasted from MongoDB Atlas in the Connect option 3 - Have you double checked if your ip is whitelisted in the Atlas Network Access settings? Dynamic IPs are a usual source of connection problems in dev setups 4 - Consider adding a .catch block to mongoose.connect, to log the reason why the connection Promise was rejected and see if it complains about something in your Uri:

mongoose.connect(uri ,connectionOptions).catch(console.error); 

It looks like a malformed uri that the mongodriver cannot parse and use to connect, but I can’t tell exactly why from the info you posted

0reactions
krishnadevzcommented, Mar 7, 2021

the issue is resolved now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhandled Promise Rejection Warning: Mongoose Server ...
First of all, check the port status in Firewall, is it open. Then you can actually in your Mongo Atlas add the whitelist...
Read more >
Unhandled Promise Rejection Warning: Mongoose ... - ADocLib
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...
Read more >
[Solved]-got an error in Unhandled promise rejection-mongodb
The two errors you received mean the same thing: your MongoDB server is not running. Double-check the installation instructions for mongo itself, ...
Read more >
server selection timed out after 30000 ms - You.com
Update your Mongoose to 5.9.2 to fix the issue. · Connections are opened from the correct IP address and there is a connection...
Read more >
UnhandledPromiseRejectionWar...
How to solve UnhandledPromiseRejectionWarning : MongooseServerSelectionError : connect ECONNREFUSED 127.0.0.1:27017 in node js and mongodb is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found