1

I have following cloud code fetching some records and then adding them to an array along with corresponding dates, but array always appears to have same date which is last date in the range.

Parse.Cloud.define("getJournalEntryWeekly", function(request, response) {
    var currDate =new Date( request.params.currDate);
    var user = request.params.user;
    var avgArray = [];
    var success = false;
    var counter = 0;
    var day=0;
    var d1;
    var d2;

    for(i = 0;i < 7; i++)
    {
        d1 = new Date(currDate.getTime() + 1000 * 60 * 60 * 24 * (i+1)); 
        d2 = new Date(currDate.getTime() + 1000 * 60 * 60 * 24 * (i));
        var query = new Parse.Query("JournalEntry");
        query.greaterThan("createdAt",d2);
        query.lessThan("createdAt",d1);
        query.equalTo("userName",user);

        query.find({
            success: function(results) {
                counter++; 
                var avg=0;
                var level = 0;
                var total = results.length;
                for(j = 0; j < results.length ; j++)
                {
                    var entry = results[j];
                    level += entry.get("happinessLevel");

                    //avgArray.push(entry.get("happinessLevel"));
                }
                if(results.length != 0)
                {
                    avg = level/total;
                }
                day++;
                avgArray.push({day:d1,avg:avg});

                if(counter == 7)
                {
                    response.success(avgArray);
                }
            },
            error: function() {
                counter++;
                //response.error("no entry found");
            }
        });
    }
});

Following is the output

{"result":[{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":63},{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":0},{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":0},{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":0},{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":0},{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":0},{"day":{"__type":"Date","iso":"2014-07-27T00:00:00.000Z"},"avg":0}]}

Date variable d1 which i m adding to response array is always at its last value , which actually should change for each array item added right from start date to end date.

1 Answer 1

1

Oversimplification:

When you call avgArray.push({day:d1,avg:avg}); you are in the asynchronously run completion callback of the prior call to find(). This runs after all of your calls to find() (in your loop) have been made. So, at this time d1 has the value of the last loop.

Accure description: javascript closures and value capture.

That link shows a good solution, which is to use a function to capture the value inside the closure. I can't say I've tried it in your specific example, and it's a complex issue, but it should point you in the right direction.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks man that article explains it going to try the solutions hope it helps me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.