Parse.com - modify all the users in background job .each() doesn't work with order and .find() finishes too fast

parse-platform

Solution

After a couple of hours researches and testing. finally find out a way to update all object in .find(). it's `Parse.Object.saveAll`. I pushed all the object i need to update into an array, then call the saveAll with a success and error block.

but Like Fosco said, only first 1000 record got returned, so i have run the background job manually for several times with a parameter of start index.

here is my code:

Parse.Cloud.job("RankingAllUserBasedOnDiamonds", function(request, status) {
    Parse.Cloud.useMasterKey();
        var usersToSave= [];
        var query = new Parse.Query(Parse.User);
        query.descending("age, name");
        query.find().then(function(results) {
            for(var i = 0; i < users.length; i++)
            {
                var user = users[i];
                user.set("age", i+1);
                usersToSave.push(user);
            }
        }).then(function() {
          Parse.Object.saveAll(usersToSave, {
            success: function(list) {
                // All the objects were saved.
                if (status) {
                    status.success("Update completed successfully.");
                };
                console.log("saveInBackground success");
            }, error: function(model, error) {
                // An error occurred while saving one of the objects.
                if (status) {
                    status.error(error);
                };
                console.log("saveInBackground error: " + error.message);
            });
        }, function(error) {
            // Set the job's error status
            status.error("ChangeUserAge went wrong.");
        });
});

Hope that can help someone has the same issue.

Problem

I am trying to get all the user data, and sort them descending by Age, then modify the age for everyone. but when i use `query.find()`, it finishes too fast. Only top 2 or 3 users age would be saved correctly. Then i try to use `query.each()`, but it doesn't work with descending order. Here are the codes i have when using query.each(), if i don't order them, it will save all the users correctly, but i need to order them and change their age based on the order. ``` Parse.Cloud.job("ChangeUserAge", function(request, status) { Parse.Cloud.useMasterKey(); var counter = 0; var query = new Parse.Query(Parse.User); query.descending("age, name"); query.each(function(user) { counter += 1; user.set("age", counter); return user.save(); }).then(function() { // Set the job's success status status.success("ChangeUserAge completed successfully."); }, function(error) { // Set the job's error status status.error("ChangeUserAge went wrong."); }); }); ``` and here are the codes i have when using query.find(), which the problem is that finishes too fast. only 2 or 3 users saved correctly. ``` Parse.Cloud.job("RankingAllUserBasedOnDiamonds", function(request, status) { Parse.Cloud.useMasterKey(); var query = new Parse.Query(Parse.User); query.descending("age, name"); query.find().then(function(results) { for(var i = 0; i < users.length; i++) { var user = users[i]; user.set("age", i+1); user.save(); } }).then(function() { // Set the job's success status status.success("ChangeUserAge completed successfully."); }, function(error) { // Set the job's error status status.error("ChangeUserAge went wrong."); }); }); ```

Original source