MongoParseError: 在解析连接字符串时,授权部分中存在未转义的 @ 符号。

11

我正在尝试使用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.

嘿,我已经回答了你的问题,请看一下。 - Saikat Chakrabortty
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
22

好的,这是因为密码中有@符号,导致程序无法确定哪部分应该被视为密码或主机, 如果您看一下Mongo字符串格式:

mongodb://<user>:<pass>@<host>:<port>/<db_name>

这里密码主机@分隔,如果还有一个@,比如在您的情况下它在密码中, Mongo字符串将变成这样:

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 Native Driver。


选项2对我不起作用。它说[pass]不受支持。 - Deepak Terse
@DeepakTerse 这是一个相当旧的评论,我正在更新它,pass 不再受支持。 - Saikat Chakrabortty
@SaikatChakrabortty 谢谢。我使用了 encodeURIComponent(),对我很有效。 - Abhilash

19

只需将@符号替换为%40即可解决它。


更改为%40后,一切都正常了,谢谢。 - Iam ByeBlogs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,