使用 Parse.com 保存查询中的对象仅限于第一行? [英] Saving objects in query with Parse.com limited to first rows?

查看:44
本文介绍了使用 Parse.com 保存查询中的对象仅限于第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题应该很常见.我有一堆用户,想根据他们的评分更新他们的排名位置.

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.

所以我创建了一个 CloudCode 函数,我调用它来更新位置.我进行查询并按其评分"对其元素进行排序.之后,我一一获取每个元素并更新它们的位置"值.

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.

代码非常简单:

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

令我惊讶的是,只有查询中的三个 firts 元素会更新它们的位置.其余元素保留在相同位置的 ddbb 中.

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

它确实表明我正在获取数据库中的所有元素,并且它们的位置应该相应地更新.但是在 a.save() 并查看 ddbb 之后,只更新了前三个元素.

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.

这有意义吗???

推荐答案

有几个可能的原因:

  • save() 操作是异步的,但您通过 response.success 提前有效地完成了该功能.
  • 如果有很多保存,您可能会达到爆发限制

无论哪种方式,将所有要保存的对象放在一个数组中,然后调用 Parse.Object.saveAll,都应该注意:

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

这篇关于使用 Parse.com 保存查询中的对象仅限于第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆