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

匿名 (未验证) 提交于 2019-12-03 00:48:01

问题:

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:azurepassword@myazuredb.documents.azure.com: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.

回答1:

For the latest version (v5.0.1) of Mongoose, you'll need to use this syntax to connect to MongoDB like this:

const mongoose = require('mongoose');  mongoose.connect('mongodb://<cosmosdb-username>.documents.azure.com:10255/<databasename>?ssl=true', {     auth: {       user: '<cosmosdb-username>',       password: '<cosmosdb-password>'     }   })   .then(() => console.log('connection successful'))   .catch((err) => console.error(err)); 


回答2:

The CosmoDB password I received through Azure ended with ==, hence the illegal characters message. However, these can be urlencoded, as they should be, in a valid URI.

An equals sign = urlencoded is %3D.

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



回答3:

To connect to local cosmos db emulator use the following connection method (for mongoose > 5.0.0):

  mongoose.connect(    `mongodb://localhost:10255/?ssl=true`,   {     auth: {       user: "localhost",       password: "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",       dbName: "admin"     }   } ); 

Or you may also do the following:

const encodedPassword = encodeURIComponent("C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="); mongoose.connect(`mongodb://localhost:${encodedPassword}@localhost:10255/admin?ssl=true`); 

Connection string has following format:

mongodb://username:password@host:port/[database]?ssl=true

and there seems to be some issue with default password character escaping. Thus we encoded it separately.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!