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

Posted by Keystion on 2018-10-28

useNewUrlParser

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

1
2
3
const mongoose = require('mongoose');

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

警告提示:

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

更改为一下代码即可:

1
2
3
4
5
6
7
8
9
const mongoose = require('mongoose');

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