【问题标题】:Mongoose: ".find(...).exec(...).then(...).catch(...).finally is not a function" using bluebird?Mongoose:“.find(...).exec(...).then(...).catch(...).finally 不是函数”使用蓝鸟?
【发布时间】:2018-03-23 06:27:27
【问题描述】:

我目前正在尝试将 Promises 与 Mongoose 一起使用。 我已经读到,从 4.1 开始,添加了 mPromise 以及插入外部 Promise 库(如 bluebird 或 q)的能力。

我对只需要然后捕获的基本承诺没有任何问题,但是,当我尝试使用 Bluebird 方法 finally 时,我最终无法这样做,并出现上述错误。这是一个代码sn-p:

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 承诺吗?

谢谢

【问题讨论】:

    标签: javascript node.js mongoose promise bluebird


    【解决方案1】:

    当然,经过几个小时的努力,我在 SO 上发帖并找到了答案 :) 感谢 Anton Novik 的回答 https://stackoverflow.com/a/42313136/8569785 在另一个线程中,我设法插入了蓝鸟。

    原来项目中的一个文件有一个

    var mongoose = require('mongoose');
    mongoose.promise = require('bluebird');
    

    在几行之后执行另一个未被注意到的任务:

    mongoose.promise = global.Promise // Effectively assigning mongoose promise to the native implementation, oops ! 
    

    删除分配后,确保每个 mongoose 分配都是本地范围的,现在解决了!

    【讨论】:

      猜你喜欢
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 2016-10-18
      • 1970-01-01
      • 2016-02-14
      • 2021-08-28
      • 2016-11-22
      相关资源
      最近更新 更多