我目前正在尝试将 Promises 与 Mongoose 结合使用。 我读到从 4.1 开始,mPromise 已经被添加,并且能够插入外部 promise 库,如 bluebird 或 q。
我对只需要 then and catch 的基本 promise 没有任何问题,但是,当我尝试使用 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));
我还确保需要 Bluebird
var Promise = require('bluebird');
var mongoose = require('mongoose');
mongoose.Promise = Promise;
知道 Mongoose 为什么不返回 Bluebird promise 吗?
谢谢
最佳答案
当然,经过几个小时的努力,我在 SO 上发帖并找到了答案:) 感谢 Anton Novik 的回答 https://stackoverflow.com/a/42313136/8569785 在另一个线程中,我设法插入了 Bluebird 。
原来项目中的一个文件有一个
var mongoose = require('mongoose');
mongoose.promise = require('bluebird');
几行之后的另一个任务被忽视了:
mongoose.promise = global.Promise // Effectively assigning mongoose promise to the native implementation, oops !
在删除分配并确保每个 mongoose 分配都是本地范围分配后,现在已解决!
关于javascript - Mongoose : ".find(...).exec(...).then(...).catch(...).finally is not a function"使用 Bluebird ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46690596/