Unhandled Promise Rejection Warning: Mongoose Server Selection Error
See original GitHub issueUnhandledPromiseRejectionWarning: 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
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:
- Created 3 years ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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:
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
the issue is resolved now