gpt4 book ai didi

node.js - Mongo解析错误: Unescaped at-sign in authority section at parseConnectionString

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:11 24 4
gpt4 key购买 nike

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

最佳答案

嗯,这是因为 @在密码中,它无法理解应该将哪一部分视为密码或主机,如果您看到 Mongo 字符串格式:

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

这里密码主机@分隔如果还有一个@进来,在你的情况下说它的内部密码,Mongo 字符串将如下所示:

mongodb://<user>:<pass_1st_part>@<pass_2nd_part>@<host>:<port>/<db_name>这是 Not Acceptable ,

为此你可以这样做:

mongoose.connect(`mongodb://<USER_NAME>:${encodeURIComponent('<P@SS>')}@<HOST>:<PORT>/<DB_NAME>`)

Here we are encoding the password, with encodeURIComponent(), so the p@ss will become p%40ss.

您可以随时替换 @%40但有时这并不是最好的方法。所以你可以使用上面的一些函数来做同样的事情。

注意:有时您可能需要添加 { uri_decode_auth: true }选项中。如果您使用 Mongo Native 驱动程序。

关于node.js - Mongo解析错误: Unescaped at-sign in authority section at parseConnectionString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54335970/

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