Nodejs / Mongodb: Deprecationwarning: Current URL string parser is deprecated, and will be Removed in a Future version

Asked

Viewed 3,054 times

2

I’m in trouble in:

let options = 'replicaSet=XPTO&readPreference=secondary';
var url = `mongodb://${user}:${password}@${host}/admin?${options}`;

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.

Even using {useNewUrlParser: true} after ${options} I still have the same problem. Can help?

  • Edit the question by adding the code snippet where you make the connection.

  • I’ve done it, it didn’t work out so well!

  • Have you edited providing the code snippet as Luiz instructed? I’m not seeing in your question

  • Yes, I’ve put the url = mongodb://${user}:${password}@${host}/admin?${options},{useNewUrlParser: true};

1 answer

3


Do not put { useNewUrlParser: true } inside your url variable, you must put it as the second parameter in the connection object. thus:

const mongoClient = require("mongodb").MongoClient;
MongoClient.connect("mongodb://localhost:27017/SeuBanco", { useNewUrlParser:true})
  .then(conn => algum código caso a conexão for ok)
  .catch(err => algum código caso de erro) 

Note that in the first line I created the mongoCliente object that I imported from the mongodb module, then I used the connect function of this object to open the connection, it is in it that you must specify the useNewUrlParser as the second parameter. The way you were doing, by putting inside your url variable it was going in the first parameter. Source: http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html#. connect I hope I’ve helped!

  • Congratulations, very well explained and I managed to solve very well! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.