连接到mongodb成功,但出现错误

时间:2018-09-17 20:34:12

标签: node.js mongodb

我已经安装了mongodb,并且在没有用户名和密码的情况下进行投放; 但是当我运行这段代码时:

    (node:7280) 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.

it throws :

    mongoose.connect(db,(err) => {
        if (err)
            console.error(err);
        else
            console.log("Connected to the mongodb");
    });

2 个答案:

答案 0 :(得分:6)

如果同样失败,则需要在代码中添加{useUnifiedTopology: true }。看起来像:

mongoose.connect(db, {useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
  if (err)
     console.error(err);
  else
     console.log("Connected to the mongodb"); 
});

答案 1 :(得分:0)

将代码更改为:

mongoose.connect(db, {useNewUrlParser: true}, (err) => {
    if (err)
        console.error(err);
    else
        console.log("Connected to the mongodb"); 
});

收到此错误是因为您使用的是MongoClient的较新版本(> = 4.0.0)