gpt4 book ai didi

javascript - Node.js:如何处理新 URL 解析器弃用警告?

转载 作者:太空宇宙 更新时间:2023-11-04 01:39:44 24 4
gpt4 key购买 nike

当我使用 mongoose 和express 运行我的node.js 服务器时,我收到此警告:

(node:27809) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pa ss option { useNewUrlParser: true } to MongoClient.connect.

当我将 { useNewUrlParser: true } 传递给我的连接选项时,我得到:

(node:27799) UnhandledPromiseRejectionWarning: TypeError: callback is not a function at $initialConnection.$initialConnection.then (node:27799) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async func tion without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:27799) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handl ed will terminate the Node.js process with a non-zero exit code.

这是我的代码:

//Mongoose Connection & Validation Process

mongoose.connect("mongodb://12.345.65.89:3000/db",
{user: 'user', pass: 'p@ssword'},{ useNewUrlParser: true });

mongoose.connection.on('connected', function () {
console.log('Mongoose connected!')
});

mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error');
});

我该如何解决这个问题?我认为这与我在 useNewUrlParser 之前传递用户和密码的方式有关,但我必须这样做,否则由于在密码中使用 @ ,我会收到错误:/有什么想法吗?

谢谢!

最佳答案

const db = require('./config/keys').mongoURI;
//mongoURI: 'mongodb://localhost:27017/mernApp'

mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("MongoDB connected"))
.catch(err => console.log(err));

关于javascript - Node.js:如何处理新 URL 解析器弃用警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53342215/

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