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

狂风中的少年 提交于 2020-01-12 07:14:18

问题


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 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.




回答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.



来源:https://stackoverflow.com/questions/48425520/error-connecting-to-azure-illegal-character-in-password-with-mongoose-5-0-1-but

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