首页 > 解决方案 > 使用 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,但是当我试图连接到该数据库时,它向我抛出了该错误。我需要在这里连接到我的数据库。

标签: node.jsmongodbmongoosemlab

解决方案


0.0.0.0 请在MongoDB网站的“主页>安全部分>网络访问>添加IP”添加您当前的IP或 白名单。

我希望这有帮助。


推荐阅读