mongodb连接警告修复

问题

Node.js中mongoose模块连接MongoDB数据库时提示(node:12580) 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');
let db = mongoose.connect('mongodb://localhost/hd15');
mongoose.connection.on("error", function (error) {
    console.log("数据库连接失败:" + error);
});
mongoose.connection.on("open", function () {
    console.log("------数据库连接成功!------");
});

解决

const mongoose = require('mongoose');
//27017是MongoDB数据库默认端口号
let db = mongoose.connect('mongodb://localhost:27017/hd15',{ useNewUrlParser: true });

mongoose.connection.on("error", function (error) {
    console.log("数据库连接失败:" + error);
});
mongoose.connection.on("open", function () {
    console.log("------数据库连接成功!------");
});

猜你喜欢

转载自www.cnblogs.com/kinblog/p/10951553.html
今日推荐