node.js - MongoParseError:在 parseConnectionString 处未转义的登录权限部分
问题描述
我正在尝试使用 mlab 将我的 nodejs 应用程序连接到 mongoDB,但我遇到了错误,这对我来说是无法理解的。
let mongoose=require('mongoose');
const server='ds366482.mlab.com:11275';
const database='customer_db'
const user='xyz'
const password='xyz@5295'
mongoose.connect(`mongodb://${user}:${password}@${server}/${database}`, {useNewUrlParser: true})
let customerSchema=new mongoose.Schema({
name:String,
email:{
type:String,
required:true,
unique:true
}
})
这是我的代码的实际结果:
server has started on 3000
(node:4800) UnhandledPromiseRejectionWarning: MongoParseError: Unescaped at-sign in authority section
at parseConnectionString (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb-core/lib/uri_parser.js:450:21)
at connect (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:180:3)
at connectOp (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
at executeOperation (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/utils.js:420:24)
at MongoClient.connect (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongodb/lib/mongo_client.js:168:10)
at Promise (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongoose/lib/connection.js:521:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongoose/lib/connection.js:518:19)
at Mongoose.connect (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/node_modules/mongoose/lib/index.js:270:15)
at Object.<anonymous> (/home/admybrand/Desktop/Gaurav's Stuff/rest-api/src/models/customer.model.js:9:10)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
(node:4800) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing insideof an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:4800) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案
好吧,这是因为@
in 密码,如果您看到 Mongo 字符串格式,它无法理解应该将哪个部分视为密码或主机:
mongodb://<user>:<pass>@<host>:<port>/<db_name>
这里密码和主机是分开的@
,如果还有一个@
进来,说在你的情况下它的内部密码,Mongo String 将如下所示:
mongodb://<user>:<pass_1st_part>@<pass_2nd_part>@<host>:<port>/<db_name>
这是不可接受的,
为此,您可以执行以下操作:
mongoose.connect(`mongodb://<USER_NAME>:${encodeURIComponent('<P@SS>')}@<HOST>:<PORT>/<DB_NAME>`)
在这里,我们使用 对密码进行编码
encodeURIComponent()
,因此p@ss
将变为p%40ss
。
您可以随时替换@
,%40
但有时这不是最好的方法。所以你可以使用上面的一些功能来做同样的事情。
注意:有时
您可能需要添加选项{ uri_decode_auth: true }
。如果您使用的是 Mongo 本机驱动程序。
推荐阅读
- algorithm - 总和中递归关系的加法在哪里?
- python - 如何区分具有相同类名的两个元素
- neural-network - 如何使用向量(1x160)输入和输出向量(1x6)实现神经网络
- mongodb - 如何将 Bson ObjectId 添加到 JObject
- firebase - Firestore 规则:如何允许基于父属性读取嵌套文档
- android - Android手势在活动中不起作用
- javascript - 解决将变量更改为特定值的承诺
- python - 派斯帕克。如何创建 df.shema 那样的?
- javascript - 转义但仍然“未捕获的语法错误:无效的正则表达式:正则表达式:未终止的组”
- reactjs - 如何将两个对象合并为一个 - React-Native 状态