current URL string parser is deprecated in mongo db and nodejs

Viewed 6542

I am using mongodb and nodejs 8 to connect to database but I get this error :

    (node:7280) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

my codes :

mongoose.connect(db,(err) => {
            if (err)
                console.error(err);
            else
                console.log("Connected to the mongodb");
        });
2 Answers

Change your code to:

mongoose.connect(db, {useNewUrlParser: true}, (err) => {
    if (err)
        console.error(err);
    else
        console.log("Connected to the mongodb"); 
});

You are getting this error because you are using a newer version (>=4.0.0) of MongoClient

If it fails too, you need to add {useUnifiedTopology: true } to the code. It looks like:

mongoose.connect(db, {useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
  if (err)
     console.error(err);
  else
     console.log("Connected to the mongodb"); 
});
Related