我正在使用 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.
Run Code Online (Sandbox Code Playgroud)
我的代码:
mongoose.connect(db,(err) => {
if (err)
console.error(err);
else
console.log("Connected to the mongodb");
});
Run Code Online (Sandbox Code Playgroud)
D__*_*___ 12
将您的代码更改为:
mongoose.connect(db, {useNewUrlParser: true}, (err) => {
if (err)
console.error(err);
else
console.log("Connected to the mongodb");
});
Run Code Online (Sandbox Code Playgroud)
您收到此错误是因为您使用的是较新版本 (>=4.0.0) 的 MongoClient
如果它也失败,则需要添加{useUnifiedTopology: true }到代码中。看起来像:
mongoose.connect(db, {useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
if (err)
console.error(err);
else
console.log("Connected to the mongodb");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5714 次 |
| 最近记录: |