2

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!

Eamon Scullion
  • 1,354
  • 7
  • 16
ikanimai
  • 47
  • 1
  • 8
  • I think this might answer your question: https://stackoverflow.com/questions/50448272/avoid-current-url-string-parser-is-deprecated-warning-by-setting-usenewurlpars – Jim B. Nov 16 '18 at 16:59

1 Answers1

0
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));
  • 3
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Mar 16 '19 at 14:04