6

When I run a simple NodeJS project to upload data to a database using MongoDB, I get the following errors:

(node:3556) 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.

Why? and how can I fix this?

d-cubed
  • 1,034
  • 5
  • 30
  • 58
Sourab Bhowmik
  • 85
  • 1
  • 1
  • 7
  • 1
    Welcome to Stack Overflow. Please share some code so we can best help you. There is information about how to ask a good question in the help centre: https://stackoverflow.com/help/how-to-ask – Chris Jul 29 '18 at 19:57

2 Answers2

26

If you're using mongo to connect try using:

MongoClient.connect('mongodb://user:password@domain.com:port/dbname', { useNewUrlParser: true });

If you're using mongoose, use something like this:

mongoose.connect('mongodb://user:password@domain.com:27017/dbname', { useNewUrlParser: true });

You could also use something like this:

const config = {
  autoIndex: false,
  useNewUrlParser: true,
};
return mongoose.connect(uri, config);

As explained by lineus:

https://github.com/Automattic/mongoose/issues/6667

d-cubed
  • 1,034
  • 5
  • 30
  • 58
Cisko Rijken
  • 444
  • 6
  • 5
1

Put this code inside your layout file

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/yourDatabase', { useNewUrlParser: true });
var Schema = mongoose.Schema;

Then you can create your Schema layout, for example:

var mySchema = new Schema({
    first_name: String, 
    last_name: String
});
Mario Minondo
  • 105
  • 1
  • 5
  • Don't you think it is better practice to put the mongoose connection in **app.js** with other configurations? Well, just thinking out loud. – Nate Nov 30 '19 at 14:30