2

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?

7
  • Are you sure your database is started? Commented Mar 26, 2021 at 5:59
  • 1
    yeah it is i have add a document from atlas dashboard. Commented Mar 26, 2021 at 6:02
  • In the directory that your mongodb is stored you have to start it with mongod --dbpath=data (That's the default) Commented Mar 26, 2021 at 6:03
  • 1
    My mongodb is not locally installed I have configured mongodb atlas online. Commented Mar 26, 2021 at 6:05
  • 1
    Yeah i have added ..access from everywhere option (0.0.0.0/0) Commented Mar 26, 2021 at 6:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I faced the same and applied your trick and found solution on my Linux Mint Cinnamon Desktop. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.