问题描述
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.connectiondb.on(’error’, console.error.bind(console, ’连接错误:’));db.once(’open’, function() { console.log(’连接成功’);})
问题解答
回答1:最新版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(’连接成功’);})回答2:
加个{useMongoClient:true}比如mongoose.connect(’mongodb://localhost/test’,{useMongoClient:true})就可以了,我是查看:http://mongoosejs.com/docs/co...拉下来最后页的The useMongoClient Option才知道的