I'm trying to connect my Nodejs application to MongoDB Atlas but it's throwing an error.
Here is the error message:
This is the error in the catch block: MongooseServerSelectionError: read ECONNRESET DB connection error: read ECONNRESET
Below is the code for my Nodejs application:
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
//db connection
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("DB Connected"))
.catch(err => {
console.log("This is the error in the catch block: " + err);
});
mongoose.connection.on("error", err => {
console.log(`DB connection error: ${err.message}`);
});
NOTE:- I declared .env file in which I have a variable MONGO_URI to hold the secret key required for connecting to MongoDB Atlas.
I'm trying to connect my Nodejs application to MongoDB Atlas but it's throwing an error.
Here is the error message:
This is the error in the catch block: MongooseServerSelectionError: read ECONNRESET DB connection error: read ECONNRESET
Below is the code for my Nodejs application:
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
//db connection
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("DB Connected"))
.catch(err => {
console.log("This is the error in the catch block: " + err);
});
mongoose.connection.on("error", err => {
console.log(`DB connection error: ${err.message}`);
});
NOTE:- I declared .env file in which I have a variable MONGO_URI to hold the secret key required for connecting to MongoDB Atlas.
Share Improve this question edited Mar 3, 2020 at 5:02 AnonSar asked Mar 3, 2020 at 3:58 AnonSarAnonSar 7062 gold badges9 silver badges27 bronze badges 2- Did you whitelist your IP address on Atlas? – Tunmise Ogunniyi Commented Mar 3, 2020 at 13:29
- Atlas requires SSL by default, does your connection string call for that? – Joe Commented Mar 3, 2020 at 22:24
1 Answer
Reset to default 7I was connected to a Wi-Fi connection that was having some policies due to which it was placing some restrictions and thus I was getting the above error.
I connected to another Wi-Fi connection ( not having any policies or restrictions ) and that fixed my problem.
Cheers!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744309460a4567879.html
评论列表(0条)