【问题标题】:current URL string parser is deprecated in mongo db and nodejs当前 URL 字符串解析器在 mongo db 和 nodejs 中已弃用
【发布时间】:2019-02-21 19:48:50
【问题描述】:

我正在使用 mongodb 和 nodejs 8 连接到数据库,但出现此错误:

    (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.

我的代码:

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

【问题讨论】:

标签: node.js mongodb


【解决方案1】:

将您的代码更改为:

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

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

【讨论】:

    【解决方案2】:

    如果它也失败了,你需要在代码中添加{useUnifiedTopology: true }。它看起来像:

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

    【讨论】:

      猜你喜欢
      • 2018-12-12
      • 2020-01-22
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2014-01-01
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多