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?
The useNewUrlParser OptionTo use the new parser, pass option { useNewUrlParser: true } to MongoClient. connect. The MongoDB Node. js driver rewrote the tool it uses to parse MongoDB connection strings.
useUnifiedTopology: False by default. Set to true to opt in to using the MongoDB driver's new connection management engine. You should set this option to true , except for the unlikely case that it prevents you from maintaining a stable connection.
To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });
If you're using mongo to connect try using:
MongoClient.connect('mongodb://user:[email protected]:port/dbname', { useNewUrlParser: true });
If you're using mongoose, use something like this:
mongoose.connect('mongodb://user:[email protected]: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
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With