0

My problem is suppossed to be very common. I have a bunch of users and want to update their ranking position according to their scoring.

So I've created a CloudCode function that I call to update ther positions. I make a query and order its elements by their "scoring". After that I obtain each element one by one and I update their "position" value.

The code is pretty simple:

Parse.Cloud.define("update_ranking", function(request, response) {
    query = new Parse.Query("Scoring");
    query.descending("scoring");
    query.find({
        success: function(results) {
            for(var i = 0; i < results.length; ++i) {
                a = results[i];
                console.log("Position of " + a.get("name") + ": " + a.get("position") + ", new position: " + i );
                a.set("position", i + 1);
                a.save();
                // a.save(null, {
                //  success: function(a) {
                //      console.log("Posicion de " + a.get("name") + ": " + a.get("position"));
                //      a.set("position", i);
                //      a.save();
                //  },
                //  error: function(a, error) {
                //      console.log('No se pudo guardar el objeto con el error: ' + error.message);
                //  }

                // });
            }
            response.success('All players updated');
        }
    });
});

My surprise is that only the three firts elements in the query get their positions updated. The rest of the elements remain in the ddbb with the same position.

If I see the console log:

I2014-09-20T11:37:51.331Z] Position of Fallen: 2, new position: 2
I2014-09-20T11:37:51.333Z] Position of Paco: 1, new position: 3
I2014-09-20T11:37:51.334Z] Position of Pepe: 19, new position: 6
I2014-09-20T11:37:51.334Z] Position of Dime: 12, new position: 1
I2014-09-20T11:37:51.334Z] Position of Otto: 14, new position: 12
I2014-09-20T11:37:51.336Z] Position of Rick: 16, new position: 11
I2014-09-20T11:37:51.337Z] Position of Charles: 17, new position: 15

it really shows that I am getting all the elements in the database and that their positions should be udpated accordingly. But after a.save() and look into the ddbb, only the first three elements are updated.

Does this makes sense???

1 Answer 1

1

There are a couple of possible reasons:

  • The save() operation is async, but you are effectively finishing the function prematurely with response.success.
  • If there are a lot of saves, you could be hitting a burst limit

Either way, putting all the objects to be saved in an array, then call Parse.Object.saveAll, should take care of it:

var toBeSaved = [];

for(var i = 0; i < results.length; ++i) {
    a = results[i];
    a.set("position", i + 1);
    toBeSaved.push(a);
}

Parse.Object.saveAll(tobeSaved, {
    success: function () {
        response.success('All players updated');
    }, error: function (err) {
        // handle error
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Tom, that was the problem. I already has solved by taking out response.success but obtained an error in the log and your solution solved everything. BTW, How much saves do you think could produce a burst limit?
Thank you Tom, but I'm not sure how many request would do my javascript function. Suppose we have 1000 players, saveAll would be one hit, or it would produce 1000 hits?
saveAll will be one hit, that's (one of the reasons) why it's useful

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.