Node.js: How to handle New URL Parser Deprecation Warning?

Viewed 7332

when i run my node.js server with mongoose and express I get this warning:

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

When i pass { useNewUrlParser: true } to my connection options i then get:

(node:27799) UnhandledPromiseRejectionWarning: TypeError: callback is not a function at $initialConnection.$initialConnection.then (node:27799) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async func tion without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:27799) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handl ed will terminate the Node.js process with a non-zero exit code.

Here is my code:

//Mongoose Connection & Validation Process

mongoose.connect("mongodb://12.345.65.89:3000/db",
         {user: 'user', pass: 'p@ssword'},{ useNewUrlParser: true });

mongoose.connection.on('connected', function () {
     console.log('Mongoose connected!')
 });

mongoose.connection.on('error',function (err) {
     console.log('Mongoose default connection error');
});

How can i fix this? I think it has something to do with how i pass the user and password before the useNewUrlParser, but i had to do it this way otherwise i get an error due to the use of @ in the password :/ Any ideas?

Thanks!

1 Answers
const db = require('./config/keys').mongoURI;
//mongoURI: 'mongodb://localhost:27017/mernApp'

mongoose
    .connect(db, { useNewUrlParser: true })
    .then(() => console.log("MongoDB connected"))
    .catch(err => console.log(err));
Related