蓝鸟:承诺没有方法传播'

时间:2015-09-01 08:00:07

标签: node.js mongodb mongoose promise bluebird

我尝试使用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 ];
    }
}

1 个答案:

答案 0 :(得分:0)

我不认为_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 ]);
      });
  });
},