避免“不推荐使用当前 URL 字符串解析器"通过将 useNewUrlParser 设置为 true 来发出警告 [英] Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true

查看:22
本文介绍了避免“不推荐使用当前 URL 字符串解析器"通过将 useNewUrlParser 设置为 true 来发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库包装类,它与某个 MongoDB 实例建立连接:

I have a database wrapper class that establishes a connection to some MongoDB instance:

async connect(connectionString: string): Promise<void> {
        this.client = await MongoClient.connect(connectionString)
        this.db = this.client.db()
}

这给了我一个警告:

(node:4833) DeprecationWarning:当前 URL 字符串解析器已被弃用,并将在未来版本中删除.要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect.

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

connect() 方法接受 MongoClientOptions 实例作为第二个参数.但它没有名为 useNewUrlParser 的属性.我还尝试在连接字符串中设置这些属性,如下所示: mongodb://127.0.0.1/my-db?useNewUrlParser=true 但它对这些警告没有影响.

The connect() method accepts a MongoClientOptions instance as second argument. But it doesn't have a property called useNewUrlParser. I also tried to set those property in the connection string like this: mongodb://127.0.0.1/my-db?useNewUrlParser=true but it has no effect on those warning.

那么我该如何设置 useNewUrlParser 来移除这些警告呢?这对我很重要,因为脚本应该作为 cron 运行,并且这些警告会导致垃圾邮件垃圾邮件.

So how can I set useNewUrlParser to remove those warning? This is important to me since the script should run as cron and those warnings result in trash-mail spam.

我在 3.1.0-beta4 版本中使用 mongodb 驱动程序,并在 3.0 中使用相应的 @types/mongodb 包.18.它们都是使用 npm install 的最新版本.

I'm using mongodb driver in version 3.1.0-beta4 with corresponding @types/mongodb package in 3.0.18. Both of them are the latest avaliable using npm install.

使用旧版本的 mongodb 驱动:

Using an older version of mongodb driver:

"mongodb": "~3.0.8",
"@types/mongodb": "~3.0.18"

推荐答案

检查你的 mongo 版本:

mongo --version

如果您使用版本 >= 3.1.0,请将您的 mongo 连接文件更改为 ->

If you are using version >= 3.1.0, change your mongo connection file to ->

MongoClient.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true })

或你的猫鼬连接文件 ->

or your mongoose connection file to ->

mongoose.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true });

理想情况下,它是第 4 版功能,但 v3.1.0 及更高版本也支持它.查看 MongoDB GitHub 了解详情.

Ideally, it's a version 4 feature, but v3.1.0 and above are supporting it too. Check out MongoDB GitHub for details.

这篇关于避免“不推荐使用当前 URL 字符串解析器"通过将 useNewUrlParser 设置为 true 来发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆