gpt4 book ai didi

node.js - 连接到 Azure : Illegal character in password with mongoose 5. 0.1 时出错,但在 4.13.9 中有效

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:58 25 4
gpt4 key购买 nike

我有一个使用 CosmosDB 和 MongoDB API 部署到 Azure 的 node.js 应用程序。我的应用程序使用 mongoose,它可以在 4.13.9 中无缝运行。

我的应用程序连接如下:

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

配置/数据库文件定义如下(更改用户名、密码、数据库以保护无辜者):

module.exports = {
'url': 'mongodb://azureusername:azurepassword@myazuredb.documents.azure.com:10255/?ssl=true'
}

当我安装 mongoose 5.0.1 时,问题就来了。我从连接中删除了 useMongoClient 选项并取消了 promise ,所以我的连接代码现在是:

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

当它运行时,我在控制台中得到以下信息:

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

我什至可以注释掉只有 mongoose.connect 的连接代码,这就是导致错误的原因。我究竟做错了什么?我需要考虑 5.0.1 中的重大变化吗?作为可能相关也可能不相关的旁注,我看到了一些关于现在提供回调而不是使用 promise 的注释,所以如果有人有他们如何在 Node/Express 应用程序中执行此操作的示例,那会很棒,但事实并非如此当我在连接上收到有关非法字符的错误报告时,这似乎不是 isee。注意:针对 4.13.9 或 5.0.1 运行时,配置文件完全相同,所以我知道密码有效,这不是问题所在。

最佳答案

对于最新版本 (v5.0.1) 的 Mongoose,您需要使用此语法连接到 MongoDB,如下所示:

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

关于node.js - 连接到 Azure : Illegal character in password with mongoose 5. 0.1 时出错,但在 4.13.9 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48425520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com