问题
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("------数据库连接成功!------");
});