首页 > 解决方案 > 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.

标签: node.jsmongodbmongoosemlab

解决方案


好吧,这是因为@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 本机驱动程序。


推荐阅读