解析承诺和循环

时间:2015-01-26 20:11:09

标签: loops parse-platform push-notification promise

我在创建的后台工作中遇到了问题。在第一个循环中一切正常,更新对象并保存。将对象存储到数组中并将其记录下来时,会出现问题:

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]

然后是在尝试发送推送时失败说" get不是方法"。我正在使用.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);

});

1 个答案:

答案 0 :(得分:0)

根据答案here

  

when的回调为每个Promise获取一个参数。

与Bluebird不同,结果会自动传播,而不是以数组形式传递。

尝试:

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

_.each()应该接受arguments。如果不是只做标准Array.prototype.slice.apply(arguments)技巧。