连接到MongoDB时Mongoose中的弃用警告

时间:2017-08-19 05:58:38

标签: node.js mongoose

我有以下代码连接到MongoDB:

*const mongoose = require('mongoose');
//Connect to mongodb
mongoose.connect('mongobd://localhost/testaroo');
mongoose.connection.once('open', function () {
console.log("Connection has been made");
}).on('error', function (error) {
console.log("Connection Error:", error);
});*

我发现了这种错误:

  

弃用警告:在{mongoose

中不推荐使用open()

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

现在不推荐使用db open方法。所以你可以尝试下面的例子

/*
 * The file will take care of the database connectivity
 */
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/testaroo',{
    useMongoClient: true,
});


//check if we are connected successfully or not
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));