2015-01-26 182 views
1

我遇到了我創建的背景作業問題。在第一個循環中一切正常,更新對象並保存。解析承諾和循環

I2015-01-26T16:00:17.861Z] Orders array is holding [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] 

I2015-01-26T16:00:17.862Z] Order contains [object Object] 

I2015-01-26T16:00:17.863Z] Order contains [object Object] 

I2015-01-26T16:00:17.864Z] Order contains [object Object] 

I2015-01-26T16:00:17.865Z] Order contains [object Object] 

I2015-01-26T16:00:17.866Z] Order contains [object Object] 

I2015-01-26T16:00:17.867Z] Order contains [object Object] 

I2015-01-26T16:00:17.868Z] Order contains [object Object] 

然後就是試圖發送一個推說當「得到的是不是方法」失敗:存儲對象時到一個數組中,並註銷它的內容,我得到發生問題。我使用.when方法返回一個解析承諾,但似乎我做錯了什麼。如果我使用for循環一切正常,我得到一個數組,我可以發出推送通知。唯一的問題是它只發送一次然後停止。其他用戶永遠不會收到推送。請幫忙。

var query = new Parse.Query(Order); 
query.equalTo("payDate", payDate); 
query.equalTo("complaintFiled", false); 
query.find().then(function(orders){ 

    var promise = Parse.Promise.as(); 

    var objects = []; 


    console.log(orders.length); 

    _.each(orders, function(order) { 

      console.log(order); 

      counter += 1; 
      order.set("sellerPaid", true); 
      order.set("fullFilled", true); 
      objects.push(order.save()); 

    }); 

    console.log("Paid " + counter + "sellers"); 
    console.log("Orders is holding " + orders.length); 
    console.log("Orders array is holding "+ orders);  

    return Parse.Promise.when(objects); 

}).then(function(orders){ 

    _.each(orders, function(order) { 

     console.log("Order contains " + order); 


      var seller  = order.get("seller"); 
      var amountOwed = order.get("amountOwed"); 
      var message = "A payment was made to your account in the amount of $" + amountOwed + " ."; 

      var query = new Parse.Query(Parse.Installation); 
      query.equalTo("user", seller); 

      Parse.Push.send({ 
       where: query, 
       data:{ 
        alert: message, 
        badge: "Increment", 
        title: "Payment Made!" 
       } 
      }, { 
       success: function(){ 

        console.log("Success"); 
       }, 
       error: function(error) { 


        response.error(errMsg); 

       } 
      }); 
    }); 

}).then(function(){ 


    status.success("Completed background job"); 


}, function(error){ 


    status.error(error); 

}); 

回答

0

根據答案here

的回調時,得到每個承諾一個參數。

所以不像說Bluebird,結果會自動傳播,不會在數組中傳遞。

嘗試:

... 
    return Parse.Promise.when(objects); 
}).then(function() {//<<< no formal vars 
    _.each(arguments, function(order) {//<<< `arguments`, not `orders` 
    ... 

​​應該接受arguments。如果不只是做標準Array.prototype.slice.apply(arguments)的伎倆。