避免使用“不赞成使用当前的URL字符串解析器".通过将useNewUrlParser设置为true发出警告 [英] Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to 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()
}
这给了我一个警告:
(节点: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.18
中的相应@types/mongodb
软件包.它们都是使用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屋!