简体   繁体   English

使用 NodeJs 连接到 MongoDB 时的错误消息

[英]Error message when connecting to MongoDB with NodeJs

I am connecting to my MongoDB with node.js.我正在使用 node.js 连接到我的 MongoDB。 The server connects to the database, but I get an annoying error message.服务器连接到数据库,但我收到一条烦人的错误消息。

This is the error:这是错误:

(node:9252) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. (node:9252) DeprecationWarning: 当前 URL 字符串解析器已被弃用,并将在未来版本中删除。 To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。

here is my code which is located in a separate file that : server.js :这是我的代码,它位于一个单独的文件中: server.js

module.exports = {
    mongoURI: 'mongodb://127.0.0.1:27017/shoppinglist'
} 

I wish to connect to mongodb without having any errors.我希望连接到 mongodb 没有任何错误。 I am running version 3.4.18我正在运行版本 3.4.18

First of all, it is not an error, it is just a warning and it would not bother the functioning.首先,这不是错误,它只是一个警告,不会影响功能。 If you don't want to see this warning, I recommend two methods.如果你不想看到这个警告,我推荐两种方法。

First, by using { useNewUrlParser: true } you can avoid this warning message.首先,通过使用{ useNewUrlParser: true }可以避免此警告消息。

 MongoClient.connect('mongodb://127.0.0.1:27017/shoppinglist', { useNewUrlParser: true });

Second, most probably you are using mongo version >= 3.1.0 and to avoid this warning you can use 3.0.x releases.其次,很可能您使用的是 mongo 版本 >= 3.1.0,为了避免此警告,您可以使用 3.0.x 版本。

 "dependencies": { "mongodb": "~3.0.8" }

The warning message suggestion should fix the problem, you just need to add:警告消息建议应该可以解决问题,您只需要添加:

{ useNewUrlParser: true }

to your MongoClient.connect to be like this:到你的 MongoClient.connect 是这样的:

MongoClient.connect('mongodb://127.0.0.1:27017/shoppinglist', { useNewUrlParser: true });

It's not an error it's a warning (related to mongo version):这不是错误,而是警告(与 mongo 版本相关):

Mongo >= 3.1.0蒙戈 >= 3.1.0

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM