Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled promise rejection: Error: URL malformed, cannot be parsed

I am new to aws and mongodb at the same time, so I'm stuck at a very basic point in trying to connect to my mongo databse, hosted on an amazon linux ec2 instance. The reason is, I'm not able to build the path to my database.

Here is what I'm trying to use:

mongoose.connect('mongod://[email protected]:27017/test' )

And here is the result of my test lambda function:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: URL malformed, cannot be parsed

I'm using mongodb 3.6.5.

like image 318
Jean-Baptiste Avatar asked May 29 '18 17:05

Jean-Baptiste


2 Answers

My issue was a more simple URI issue. Since there was an @ character in the mongod address.

I had to use this:

return mongoose.connect(encodeURI(process.env.DB_CONNECT)); //added ');'
like image 132
Jean-Baptiste Avatar answered Oct 05 '22 19:10

Jean-Baptiste


Mongoose 5.x supports following syntax for authorization and also make sure you have not used any special character in url like @,-,+,>

mongoose.connect(MONGO_URL, {
  auth: {
    user: MONGO_DB_USER,
    password: MONGO_DB_PASSWORD
  }
})

Or if you want to remove deprication warning Avoid “current URL string parser is deprecated"

Add option useNewUrlParser

mongoose.connect(MONGO_URL, {
  auth: {
    user: MONGO_DB_USER,
    password: MONGO_DB_PASSWORD
  },
  { useNewUrlParser: true }
})
like image 25
Ashh Avatar answered Oct 05 '22 19:10

Ashh