-1

Im new to javascript, but I'm not quite sure how I'm missing a Success / Error call, I looked through and it looks all called to me.

Parse.Cloud.job("UpdateWeekData", function(request, response) {
var weeks = [];
var weekCount = 0;
var weekQuery = new Parse.Query("WeekList");

weekQuery.find({
    success: function(results) {
        for (week in results) {
            var weekCount = Parse.Query("Schedule");
            weekQuery.equalTo("weekObj", week);
            weekQuery.count({
                success:function(count) {
                    var max = week.get("maxAppts");
                    week.set("apptsRemain", (max - count));
                    week.set("numApptsSch", count);
                    week.save();
                },
                error: function(error) {
                    response.error(error.message);
                }
            });
        }
    } ,
    error: function(error) {
        response.error(error.message);
    }
});
});

Any idea where I'm missing the call? Parse Cloud Code is saying "success/error was not called"

trever
  • 961
  • 2
  • 9
  • 28

1 Answers1

0

As it is background job, you should call status.error() or status.success() as in this example.

The problem in your code is, that you are actually not calling status.success() anywhere. You must always call status.error() or status.success() when your function is finished. Calling Parse Query in for each loop makes it a little complicated (and it can make lot of requets!) beacuse you are not waiting for weekQuery.count to complete. Also using count is generally not recommended.

You should also learn how promises works so you know how to wait for queries and call error/success when everything is done - if you call success/error before query finishes, it will be aborted.

Maybe you should implement your own counting logic in database or you can make query where weekObj equals to any of results, find all objects and then for each retrieved object you can get "weekObj" field to know which one of results is it equal to. But remember that find returns 100 object by default and maximum 1000 objects (if you are using parse.com and not Parse Server yet):

weekQuery.equalTo("weekObj", results);
weekQuery.find();
David Riha
  • 1,368
  • 14
  • 29