mongoose 连接 MongoDB 数据库的 useNewUrlParser 警告解决办法

useNewUrlParser

用下面这段代码连接MongoDB数据库出现了警告提示:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

警告提示:

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

更改为一下代码即可:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true }, (err,res) => {
    if (!err) {
        console.log('连接数据成功!')
    } else {
        console.log('连接数据失败!');
    }
})

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注