Mongoose model.find({}( 在使用 mongoose 4.11 后不起作用;
我注意到猫鼬在更新到猫鼬 4.11 后行为不正常,将警告显示更新到我的屏幕后
DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead
所以我试了
let mongoConnectionLocal = {
'url': 'mongodb://username:password@localhost:27017/image-upload-gcs'
};
mongoose.connect(mongoConnectionLocal.url, { auth:{authdb:"admin"}, useMongoClient: true}, err => { if(err) { console.log(err.stack); }});
然后出现另一个警告
the options [auth] is not supported
所以我试过了,
mongoose.connect('mongodb://username:password@localhost:27017/image-upload-gcs?authSource=admin', { useMongoClient: true}, err => { if(err) { console.log(err.stack); }});
现在猫鼬不再工作了,甚至无法使用 model.find({}( 或创建文档
我尝试恢复到猫鼬 4.10,一切正常。那么这是猫鼬 4.11 及更高版本中的错误吗????
你可以试试下面的代码片段
mongoose.connect(mongoConnectionLocal.url);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function callback(){
console.log("Connection with database succeeded.");
});
var uri = 'mongodb://localhost/user1';
var promise = mongooose.connect(uri,{
useMongoClient: true,
});
promise.openUri(uri,function(errr,db){
if(errr){
throw errr;
}else{
console.log("Connection Successfull");
mydb = db;
}
});
需要对最新版本的猫鼬使用承诺进行数据库连接。您使用的是旧方法进行连接,因此请切换到较新的连接方法。
链接