Saving objects in query with Parse.com limited to first rows?

醉酒当歌 提交于 2020-01-17 05:23:12

问题


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:


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
    }
});


来源:https://stackoverflow.com/questions/25948465/saving-objects-in-query-with-parse-com-limited-to-first-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!