无法使用Nodejs连接Mongodb数据库



我试图用Nodejs连接Mongodb数据库,但它显示了以下错误:

Error MongoServerSelectionError: connection <monitor> to 18.235.147.2:27017 closed
at Timeout._onTimeout (D:BackendNodeJsPapersBackendnode_modulesmongodblibcoresdamtopology.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
}
}

我无法得到确切的问题是什么下面是我的代码:

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;

我做错了什么?

我只需在Mongodb Atlas仪表板中从access from anywhereadd current ip进行网络访问就解决了这个问题。我不知道这是Mongodb方面的问题,还是有其他办法。

最新更新