Jean-Baptiste
Jean-Baptiste

Reputation: 1562

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.

Upvotes: 9

Views: 29912

Answers (8)

Zeghra
Zeghra

Reputation: 477

I know this question has accepted answer, but this is what worked for me: I'm using Mongoose 6.0.5 and Mongodb 5.0.6, with authentication enabled and with special character (%) in the password:

mongoose.connect('mongodb://localhost:27017', {
  auth: { username: "myusername", password: "mypassword%" },
  dbName: "mydbname",
  authSource: "mydbname",
  useNewUrlParser: true,
  useUnifiedTopology: true,
}, function(err, db) {
  if (err) {
    console.log('mongoose error', err);
  }
});

Many solutions had only user and pass for auth that needed username and password instead. Also it needed dbName to get access to mydb's collections.

Upvotes: 0

Naved Khan
Naved Khan

Reputation: 1947

I have same problem but problem with password should'nt special character

password not use like this  Admin@%+admin.com wrong


password use like this  Admin  right

or any password you wanna use

Upvotes: -1

Gabriel Arghire
Gabriel Arghire

Reputation: 2370

If you deployed your app to Heroku make sure you updated the Config Vars as they are in your .env file. At least, this was my case.

Upvotes: 0

Jean-Baptiste
Jean-Baptiste

Reputation: 1562

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 ');'

Upvotes: 11

Ipsita Rout
Ipsita Rout

Reputation: 5219

In my case the below worked fine.

Inside db.js

const mongoose = require('mongoose');
const MONGODB_URI = "mongodb://host-name:27017/db-name?authSource=admin";
const MONGODB_USER = "mongouser";
const MONGODB_PASS = "myasri*$atIP38:nG*#o";

const authData =  {
    "user": MONGODB_USER,
    "pass": MONGODB_PASS,
    "useNewUrlParser": true,
    "useCreateIndex": true
}; 
mongoose.connect(
    MONGODB_URI, 
    authData,
    (err) => {
        if (!err) { console.log('MongoDB connection succeeded.'); }
        else { console.log('Error in MongoDB connection : ' + JSON.stringify(err, undefined, 2)); }
    }
);

Note:

My Node version is 10.x

MongoDb server version is 3.6.3

mongoose version is ^5.1.2

Upvotes: 2

Bryan Kimani
Bryan Kimani

Reputation: 921

If you used the following URI in your environment file for example MongoDB://<dbuser>:<dbpassword>@ds055915.mlab.com:55915/fullstack-vue-graphql Make sure your password inMONGOD_URI does not have a special character like @. I had used @ as part of my password character and was getting the error. After I removed special characters from my DB Password, all worked as expected.

Upvotes: 2

Ashh
Ashh

Reputation: 46481

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 }
})

Upvotes: 25

Sukma Saputra
Sukma Saputra

Reputation: 1599

I just want update the answer from @anthony-winzlet, because I have same error and I has solve with this code.

mongoose.connect(url, {
  auth: {
    user:'usrkoperasi',
    password:'password'
  },
  useNewUrlParser:true
}, function(err, client) {
  if (err) {
    console.log(err);
  }
  console.log('connect!!!');
});

I just add callback and useNewUrlParser:true. I use "mongoose": "^5.2.7",.

Happy coding!

Upvotes: 0

Related Questions