node.js - [mongoose] Connection warning: `open()` is deprecated in mongoose >= 4.11.0
滿天的星座
滿天的星座 2017-07-06 10:37:05
0
2
2517

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

reply all(2)
过去多啦不再A梦

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

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!