node.js - 使用 Node.js 连接到 mongoDB 数据库时出错
问题描述
使用 Node.js 连接 MLAB 中存在的 mongoDB 数据库时出现以下错误。
Error in DB connection : {
"name": "MongoNetworkError",
"errorLabels": [
"TransientTransactionError"
]
}
这是我的代码:
var mongoose = require('mongoose');
const authData = {
"useNewUrlParser": true,
"useCreateIndex": true
};
//connecting local mongodb database named test
mongoose.connect(
'mongodb://subhra:*****@ds139989.mlab.com:39989/hlloyd',
{useCreateIndex: true, useNewUrlParser: true,useUnifiedTopology: true },
(err)=>{
if (!err)
console.log('MongoDB connection succeeded.');
else
console.log('Error in DB connection : ' + JSON.stringify(err, undefined, 2));
}
);
module.exports = mongoose;
在这里,我的数据库存在于内部MLAB
,但是当我试图连接到该数据库时,它向我抛出了该错误。我需要在这里连接到我的数据库。
解决方案
0.0.0.0
请在MongoDB网站的“主页>安全部分>网络访问>添加IP”添加您当前的IP或 白名单。
我希望这有帮助。
推荐阅读
- java - 使用 Junits 测试 springboot 应用程序(需要自动装配的构造函数的类的测试方法)
- javascript - 让 Jetty Websocket 使用与 JavaScript Websocket 相同的语言
- mongodb - Go Mongo Driver 检索无模式文档
- c# - 如何使用 MVVM 模式在 WPF 中正确实现 ICommand?
- laravel - 无法让 Laravel Homestead 在 MacOS Catalina 下工作,VM 中的随机内核恐慌
- python - 如何让 Google 的 Oauth 屏幕加载到 Heroku 上 Flask 应用程序的窗口中
- php - CodeIgniter:获取 JS 文件时出现 404 错误
- java - Spring Batch - 无法在 Postgres 上创建元数据表并将实际数据加载到 mysql
- c# - 如何缩短 C# 中的大量 IF 语句?
- python - 绘制遍历列名的 Pandas 数据框(作为变量)