node.js - 无法使用 Nodejs 连接 Mongodb 数据库
问题描述
我正在尝试将 Mongodb 数据库与 Nodejs 连接,但它显示以下错误:
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
}
}
我无法得到下面的确切问题是我的代码:
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 anywhere
从add current ip
. 我不知道这是Mongodb方面的问题还是有其他出路。
推荐阅读
- git - 在我正在完成一些工作的功能分支上为 github 开发一个分叉的 repo
- list - 使用所有可能性在 Haskell 中旋转列表
- jquery - 一次将 4 个 div (从 div 列表中)附加到单独的 div 中
- postgresql - postgres的右连接返回意外结果
- javascript - 可以在字符串上使用 for in 循环吗?
- python - 日期是列,时间是行。我无法同步两者
- javascript - Vuex getter 反应性问题(结合 Vue 路由器)
- internet-explorer - ie 11 最新版本在屏幕后面打开弹幕
- html - 如何更改 pickerInput 元素的容器颜色?
- powerbi - 具有用户输入的 DAX PowerBI 平均值总和