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,471
  • 17
  • 87
  • 173
satya
  • 3,380
  • 7
  • 42
  • 119

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
  • 2,575
  • 2
  • 10
  • 23
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
  • 1,870
  • 1
  • 11
  • 15