Reputation: 3271
I am trying to connect Mongodb database with Nodejs, but it's showing below error:
Error MongoServerSelectionError: connection <monitor> to 18.235.147.2:27017 closed
at Timeout._onTimeout (D:\Backend\NodeJs\PapersBackend\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'cluster0-shard-00-01.ef0q5.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02.ef0q5.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-00.ef0q5.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
I am unable to get what exactly issue is below is my code:
const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const dburl = 'mongodb+srv://digi18:<myPasssword>@cluster0.ef0q5.mongodb.net/MyDb?
retryWrites=true&w=majority';
router.use(express.json());
router.use(express.urlencoded({ extended: true }));
router.post('/register',(req,res) => {
MongoClient.connect(dburl,{ useNewUrlParser:true,useUnifiedTopology: true },(err,client) => {
if(err){
console.log("Error",err);
}
else{
res.send("Connect");
}
});
});
module.exports = router;
What am I doing wrong?
Upvotes: 2
Views: 290
Reputation: 3271
I resolved this issue by simply making network access in Mongodb Atlas dashboard from access from anywhere
to add current ip
. I don't know whether it's an issue from Mongodb side or there is some other way out.
Upvotes: 1