猫鼬:“.find(...).exec(...).then(...).catch(...).finally 不是函数”使用蓝鸟?
Mongoose: ".find(...).exec(...).then(...).catch(...).finally is not a function" using bluebird?
我目前正在尝试将 Promises 与 Mongoose 结合使用。
我读到从 4.1 开始,mPromise 已经被添加,并且能够插入外部承诺库,如 bluebird 或 q。
我对基本承诺没有任何问题,只需要 then and catch 但是,当我尝试使用 finally
,一种 Bluebird 方法时,我最终无法这样做,并出现上述错误.这是一个代码片段:
mongoose.connect(uri, { useMongoClient: true, promiseLibrary: require('bluebird')})
.then(() => {
MyModel.find(query).exec()
.then(res => resolve(res)
.catch(err => reject(err))
.finally(() => {
mongoose.connection.close();
});
})
.catch(err => console.error(err));
我还确保需要蓝鸟
var Promise = require('bluebird');
var mongoose = require('mongoose');
mongoose.Promise = Promise;
知道为什么 mongoose 不返回 Bluebird promise 吗?
谢谢
当然,经过几个小时的努力,我 post 找到了答案 :)
感谢 Anton Novik 的回答
在另一个线程中,我设法插入了蓝鸟。
事实证明项目中的一个文件有一个
var mongoose = require('mongoose');
mongoose.promise = require('bluebird');
几行之后的另一个作业被忽视了:
mongoose.promise = global.Promise // Effectively assigning mongoose promise to the native implementation, oops !
删除分配并确保每个 mongoose 分配都是本地范围分配后,现在已解决!
我目前正在尝试将 Promises 与 Mongoose 结合使用。 我读到从 4.1 开始,mPromise 已经被添加,并且能够插入外部承诺库,如 bluebird 或 q。
我对基本承诺没有任何问题,只需要 then and catch 但是,当我尝试使用 finally
,一种 Bluebird 方法时,我最终无法这样做,并出现上述错误.这是一个代码片段:
mongoose.connect(uri, { useMongoClient: true, promiseLibrary: require('bluebird')})
.then(() => {
MyModel.find(query).exec()
.then(res => resolve(res)
.catch(err => reject(err))
.finally(() => {
mongoose.connection.close();
});
})
.catch(err => console.error(err));
我还确保需要蓝鸟
var Promise = require('bluebird');
var mongoose = require('mongoose');
mongoose.Promise = Promise;
知道为什么 mongoose 不返回 Bluebird promise 吗?
谢谢
当然,经过几个小时的努力,我 post 找到了答案 :)
感谢 Anton Novik 的回答
事实证明项目中的一个文件有一个
var mongoose = require('mongoose');
mongoose.promise = require('bluebird');
几行之后的另一个作业被忽视了:
mongoose.promise = global.Promise // Effectively assigning mongoose promise to the native implementation, oops !
删除分配并确保每个 mongoose 分配都是本地范围分配后,现在已解决!