首页  >  问答  >  正文

node.js - 【mongoose】连接警告:`open()` is deprecated in mongoose >= 4.11.0

mongoose 连接成功,但是发出警告,好像是新版本要修改哪?错误信息如下

DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0,use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`

网上找了半天没有结果,求答案,附上连接代码

var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection

db.on('error', console.error.bind(console, '连接错误:'));
db.once('open', function() {
    console.log('连接成功');
})
滿天的星座滿天的星座2632 天前2507

全部回复(2)我来回复

  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-06 10:39:05

    最新版mongoose的已知问题,你可以先降级至4.10.8版,或者试试下面的方法不知道有没有用:

    var mongoose = require('mongoose');
    mongoose.Promise = global.Promise;
    
    let db = mongoose.createConnection('mongodb://localhost/test');
    
    db.on('error', console.error.bind(console, '连接错误:'));
    db.once('open', function() {
        console.log('连接成功');
    })

    回复
    0
  • phpcn_u1582

    phpcn_u15822017-07-06 10:39:05

    加个{useMongoClient:true}
    比如
    mongoose.connect('mongodb://localhost/test',{useMongoClient:true})
    就可以了,
    我是查看:
    http://mongoosejs.com/docs/co...
    拉下来最后页的The useMongoClient Option才知道的

    回复
    1
  • 取消回复