node.js连接mongodb报错

在node.js中连接mongodb时,

mongoose.connect('mongodb://localhost:27018', function(error){
    // 连接数据库之后的回调
    if(error){
        console.log('连接数据库失败');
        console.log(error);
    }
    else{
        console.log('连接数据库成功');
        // 如果连接成功,就可以通过mongoose模块操作数据库

        //连接成功,再启动服务
        app.listen(8080);
    }

});

报错 :

(node:12596) DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection(). See http://mongoosejs.com/docs/co...
连接数据库成功
(node:12596) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/pr...

阅读 4k
2 个回答

报错都给出答案了。
你使用的open()方法已经不再支持了,使用openUri()来替代它。或者设置一下useMongoClient 的选项如果你使用connect()或createConnection()方法的话。

错误信息很关键,基本都告诉你怎么解决这个错误了。

const mongoose = require('mongoose');
const mongoStore = require('connect-mongo')(session);

// mongoose ?
let dbUrl = 'mongodb://127.0.0.1:27017/login-registration';
mongoose.connect(dbUrl, { useMongoClient: true });
mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'Mongodb connect error !'));
db.once('open', function() {
    console.log('Mongodb started !');
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题