Correct ordering in a loop using Parse

I want to create an array containing objects and I am using Parse to query all data.

However, a for loop that loops through the results does not do so in the correct order, but loops over the data randomly. If I log i

each iteration, the logs show different results each time.

Here is my code:

for (var i = 0; i < results.length; i++)
{
Parse.Cloud.useMasterKey();
// retrieve params
var objectid = results[i];
var self = request.params.userid;

// start query
var Payment = Parse.Object.extend("Payments");
var query = new Parse.Query(Payment);

query.get(objectid, {

    success: function (payment) {

        // get all the correct variables
        var from_user_id = payment.get("from_user_id");
        var to_user_id = payment.get("to_user_id");
        var amount = payment.get("amount");
        var createdAt = payment.updatedAt;
        var note = payment.get("note");
        var img = payment.get("photo");
        var location = payment.get("location");
        var status = payment.get("status");

        var fromquery = new Parse.Query(Parse.User);
        fromquery.get(from_user_id, {

            success: function(userObject) {

                var fromusername = userObject.get("name");

                var currency = userObject.get("currency");

                var toquery = new Parse.Query(Parse.User);
                toquery.get(to_user_id, {

                    success: function(touser)
                    {

                        var tousername = touser.get("name");

                        if(tousername !== null || tousername !== "")
                        {
                            sendArray(tousername);
                        }

                    },
                    error: function(touser, error)
                    {

                        var tousername = to_user_id;

                        if(tousername !== null || tousername !== "")
                        {
                            sendArray(tousername);
                        }

                    }

                });

                function sendArray(tousername) {

                    var array = new Array();

                    // create the time and date
                    var day = createdAt.getDate();
                    var year = createdAt.getFullYear();
                    var month = createdAt.getMonth();
                    var hour = createdAt.getHours();

                    var minutes = createdAt.getMinutes();

                    // create the timestamp
                    var time = "" + hour + ":" + minutes;
                    var date = "" + day + " " + month + " " + year;

                    var associativeArray = {};

                    if(self == from_user_id)
                    {
                        fromusername = "self";
                    }
                    if(self == to_user_id)
                    {
                        tousername = "self";
                    }

                    associativeArray["from"] = fromusername;
                    associativeArray["to"] = tousername;
                    associativeArray["amount"] = amount;
                    associativeArray["currency"] = currency;
                    associativeArray["date"] = date;
                    associativeArray["time"] = time;
                    associativeArray["status"] = status;

                    if(note == "" || note == null)
                    {
                        associativeArray["note"] = null;
                    }
                    else
                    {
                        associativeArray["note"] = note;
                    }

                    if(img == "" || img == null)
                    {
                        associativeArray["img"] = null;
                    }
                    else
                    {
                        associativeArray["img"] = img;
                    }

                    if(location == "" || location == null)
                    {
                        associativeArray["location"] = null;
                    }
                    else
                    {
                        associativeArray["location"] = location;
                    }

                    array[i] = associativeArray;

                    if((i + 1) == results.length)
                    {
                        response.success(array);
                    }

            },
            error: function(userObject, error)
            {
                response.error(106);
            }

        });

    },
    error: function(payment, error) {

        response.error(125);

    }

});
}

      

But the i

var parameter is always seven, so associative arrays are added to array [7] instead of the correct one i

(eg 1,2,3,4,5)

The reason this is so important is because I want to order the payment chronologically (which I did in the query providing the results).

What can I do to fix this problem?

+3


source to share


1 answer


Success is a callback that happens at a later point in time. So what happens is the for loop runs 7 times and calls parsing 7 times. Then, after it does each of the syntax attempts, calls are made, they look at i, which is now 7.

An easy way to fix this is to wrap the whole thing in a direct function and create a new closure for i. Something like that



for(var i = 0; i < results.length; i++){
   function(iClosure) {
      //rest of code goes here, replace i with iClosure
   }(i);
}

      

What now happens is that each success function will have access to its own iClosure variable, and they will be set to i at the point they were created in the loop.

+2


source







All Articles