0

I am getting the following error while connecting the mongoDB database present in MLAB using Node.js.

Error in DB connection : {
  "name": "MongoNetworkError",
  "errorLabels": [
    "TransientTransactionError"
  ]
}

Here is my code:

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;

Here my database is present inside MLAB but when I am tring to connect to that DB its throwing me that error. I need to connect to my database here.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130

2 Answers2

0

"useCreateIndex": true and useUnifiedTopology: true is deprecated .



Try out the following code to connect your mongoDB database .

mongoose.connect('mongodb://subhra:*****@ds139989.mlab.com:39989/hlloyd', {useNewUrlParser: true})
.then(() => console.log("Connected"))
.catch(err => console.log(err));

module.exports = mongoose;
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
0

Please add your current IP or 0.0.0.0 to whiteList following "main page > security section > network access > add IP" in MongoDB website.

I hope this helps.

Bivin Vinod
  • 2,210
  • 1
  • 12
  • 15