如何使用Parse.promise javascript [parse.com]定义一个promises数组并得到一个结果数组

时间:2014-12-23 04:52:40

标签: javascript parse-platform promise cloud-code

我正在尝试从一系列承诺中获取一系列结果。 我能够通过我没有获得结果数组来定义承诺数组 以下是我的代码

function get_order_selection(order){
var itemList=[];
var promise = Parse.Promise.as();
var query = new Parse.Query("Order_Selection");
query.equalTo("order", order);
query.include('wine');
promise = query.find().then(function(results) {
    console.log("Order_Selection received for order.id: " + order.id + " result(s): " +results.length);
    console.log(results);

    var i = 0;
    var size = results.length;
    while(i<size){
        var name = results[i].get('wine').get('name');
        var unitPrice = results[i].get('unitPrice');
        var quantity = results[i].get('quantity');
        var smallURL = results[i].get('wine').get('small_picture').url();
        console.log("Order_Selection-> name: "+name+ " unitPrice: "+unitPrice+" quantity: "+quantity+" smallURL: "+smallURL);
        var item = {
            name: name,
            unitPrice: unitPrice,
            quantity: quantity,
            smallURL: smallURL
        }
        itemList.push(item);
        i++;
    }
    console.log("get_order_selection: itemList:"+itemList);
    return itemList;
});
return promise;
 }

在我的主要电话中

        var i = 0;
        var size = results.length;
        while(i<size){

            selection_query.push(get_order_selection(results[i]));

            i++;
        }

我无法在阵列中得到结果。

            Parse.Promise.when(selection_query).then(
              function(results,results2,results3){
                console.log("Promise results");
                console.log("Promise results.length:"+results.length);
                 console.log("Promise results2.length:"+results2.length);
                var responseJSON = {orders: orders_list};
                response.success(responseJSON);
              }
        );

1 个答案:

答案 0 :(得分:1)

我找到了答案。 的console.log(&#34;的arguments.length:&#34 +的arguments.length);

使用参数。

        Parse.Promise.when(selection_query).then(
          function(results,results2,results3){
            console.log("arguments.length:"+arguments.length);
            console.log("Promise results");
            console.log("Promise results.length:"+results.length);
             console.log("Promise results2.length:"+results2.length);
            var responseJSON = {orders: orders_list};
            response.success(responseJSON);
          }
    );