【问题标题】:Bluebird: Promise has no method 'spread' [duplicate]蓝鸟:承诺没有“传播”的方法[重复]
【发布时间】:2015-11-26 09:16:45
【问题描述】:

我正在尝试使用 Bluebird 的 spread 方法链接一系列承诺。当我运行它时,它会引发以下错误:

Object #<Promise> has no method 'spread'

代码如下:

{
  new : function( req, next ) {
    var packet = req.body;
    var new_doc = { some obj data };

    switch (packet.event.name) {
      case "liked":
        this._create(req, new_doc )
          .spread( this._update.user )
          .spread( this._update.chirp ) // omitted from this question
        break;
      // other cases...
    };
  },
  _create: function( req, new_doc ) {

    return model_interaction.create( new_doc, function (err, interaction) {
      if (err) return next(err);
      return [ req, interaction ];
    });

  },
  _update: {
    user: function ( req, interaction ) {

      req.user._interactions['_' + interaction.event.name].push(interaction._id);
      req.user.save();

      return [ req, interaction ];
    }
}

【问题讨论】:

  • 使用版本 bluebird version 2.9.34
  • 好吧,afaik mongo 不会返回 Bluebird 的承诺……

标签: node.js mongodb mongoose promise bluebird


【解决方案1】:

我不认为_create 正在返回一个promise 对象,如果是,我不知道为什么它有一个节点样式的回调,我会将它更改为:

_create: function( req, new_doc ) {
 return new Promise(function(resolve, reject){
      model_interaction.create( new_doc, function (err, interaction) {
          if (err) return reject(err);
          resolve([ req, interaction ]);
      });
  });
},

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多