javascript - Mongoose : ".find(...).exec(...).then(...).catch(...).finally is not a function"使用 Bluebird ?

标签 javascript node.js mongoose promise bluebird

我目前正在尝试将 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/

相关文章:

javascript - 超简单的 Chrome 扩展不会将 EventListener 添加到按钮 onclick 事件

javascript - 是否可以根据屏幕尺寸设置表格选项?

javascript - event.preventDefault() 与 return false(无 jQuery)

mysql - 用于 Sequelize 模型的 Mongoose 模式

javascript - 单击查询中的链接时仅动画 div 而不是整个页面

javascript - 如何正确使用 Redis 与 Koa (node.js)

html - Node.js HTML 和 CSS

node.js - Node 异步与同步

node.js - mongoose findOneAndUpdate 返回两次,第一次为 null?

node.js - Node express Mongoose : writing API to search data by multiple optional params