5

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");
        });
Artin Zareie
  • 160
  • 3
  • 14
  • Welcome to SO. It looks like you may have forgotten to include the error you're seeing in your question. – Kryten Sep 17 '18 at 20:40
  • yes it is true , the error is : (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. – Artin Zareie Sep 17 '18 at 20:54
  • thank's for remembering – Artin Zareie Sep 17 '18 at 20:55
  • 1
    Possible duplicate of [Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true](https://stackoverflow.com/questions/50448272/avoid-current-url-string-parser-is-deprecated-warning-by-setting-usenewurlpars) – dnickless Sep 17 '18 at 21:06

2 Answers2

12

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

D_________
  • 563
  • 5
  • 14
7

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"); 
});