Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoParseError: Invalid connection string

I am trying to connect MongoDB database with this code but when running it I get the error (see the error below after the code). The initial error was in the line where it was resolved by adding useNewUrlParser: true but even after this I still get more errors. I am using MongoDB version 4.0.1. Does anybody know how to resolve this error?

mongoose.connect('User://localhost:27017/User',{ useNewUrlParser: true })

Error while running this code:

(node:11068) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string (node:11068) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

like image 545
Vishwa Sai Avatar asked Feb 16 '19 08:02

Vishwa Sai


4 Answers

Instead of User://localhost, use mongodb://localhost/ I had the same problem.

like image 150
Vail Williams Avatar answered Oct 18 '22 03:10

Vail Williams


I was receiving the same error, then I used:

mongoose.connect("mongodb://localhost:27017/[yourDbName]", {
  useUnifiedTopology: true,
  useNewUrlParser: true
});

Substitute [yourDbName] for your MongoDB database's name:

like image 43
Giovani Avatar answered Oct 18 '22 03:10

Giovani


The host you have written is not correct, and it should be

mongoose.connect('mongodb://localhost:27017/User',{ useNewUrlParser: true })
like image 21
Ahmad Sharif Avatar answered Oct 18 '22 03:10

Ahmad Sharif


Try this and it should work,

mongoose.connect('mongodb://localhost/mycargarage', {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log('MongoDB Connected...'))
    .catch((err) => console.log(err))
like image 2
Rafael Avatar answered Oct 18 '22 01:10

Rafael