Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error connecting to Azure: Illegal character in password with mongoose 5.0.1 but works in 4.13.9

I have a node.js application that is deployed to azure using CosmosDB and the MongoDB API. My application uses mongoose which works seamlessly in 4.13.9.

My application that works connects as follows:

var configDB = require('./config/database');
var mongoose = require('mongoose');
mongoose.connect(configDB.url, { useMongoClient: true } );
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

the config/database file is defined as follows (changed username, password, DB to protect the innocent):

module.exports = {
    'url': 'mongodb://azureusername:[email protected]:10255/?ssl=true'
}

Now the problem comes when I install mongoose 5.0.1. I remove the useMongoClient option from the connect and got rid of the promise so my connect code is now:

mongoose.connect(configDB.url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

When this runs I get the following in the console:

(node:21392) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Password contains an illegal unescaped character

I can even comment out the connection code to where it is only the mongoose.connect and that is what is giving the error. What am I doing wrong? Is there a breaking change in 5.0.1 that I need to account for? As a side note that may or may not be related, I saw some notes about now giving a callback instead of using promises so if someone has an example of how they do that in a Node/Express app that would be great, but it doesn't seem like that's the isee when I'm getting an error reported on the connect about an illegal character. NOTE: The config file is exactly the same when running against 4.13.9 or 5.0.1 so I know the password is valid and it is not the issue.

like image 492
Chris H Avatar asked Jan 24 '18 15:01

Chris H


1 Answers

The password for the Azure Cosmos DB instance I got ended with ==, hence the illegal characters message. These characters must be urlencoded.

An equals sign = urlencoded is %3D.

A properly encoded connection string for the password jitsu== could look like mongodb://user:jitsu%3D%3D@localhost:27017/dbname?ssl=false.

Also be aware that the connection strings you get from the Cosmos DB blade in the Azure Portal doesn't include the database name.

like image 136
sshow Avatar answered Oct 10 '22 20:10

sshow