search

Home  >  Q&A  >  body text

node.js - [mongoose] Connection warning: `open()` is deprecated in mongoose >= 4.11.0

mongoose The connection is successful, but a warning is issued. It seems that the new version needs to be modified? The error message is as follows

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

I have been searching online for a long time with no results. Please give me the answer and attach the connection code

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('连接成功');
})
滿天的星座滿天的星座2701 days ago2557

reply all(2)I'll reply

  • 过去多啦不再A梦

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

    Known issues with the latest version of mongoose, you can downgrade to version 4.10.8 first, or try the following method to see if it helps:

    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('连接成功');
    })

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-07-06 10:39:05

    Add {useMongoClient:true}
    For example
    mongoose.connect('mongodb://localhost/test',{useMongoClient:true})
    That’s it.
    I checked:
    http://mongoosejs.com/ docs/co...
    Pull down The useMongoClient Option on the last page to find out

    reply
    1
  • Cancelreply