在Before_Save中解析云代码更新计数器变量

时间:2015-02-17 23:58:18

标签: javascript parse-platform cloud-code

我使用Parse云代码在某些类的beforeSave / afterDelete中更新用户的某些计数器。用户有一个计数器,可以跟踪他们的订阅数量。订阅类是用户与另一个类之间的关系类。用户订阅计数在订阅beforeSave和afterDelete之前更新。

Parse.Cloud.beforeSave("Subscription", function(request, response) {
    var fromUserPointer = subscription.get("fromUser");

    fromUserPointer.fetch().then(function(user){

        var subscriptionCount = user.get("subscriptionCount");

        console.log( subscriptionCount + 1);
        user.set("subscriptionCount", subscriptionCount + 1);
        return user.save();
    });
});

在客户端iPhone应用程序上,用户可以浏览tableView并快速创建订阅对象。不幸的是,上面的代码似乎无法跟上。似乎如果订阅添加得太快,则在前一个before_save保存用户之前调用行var subscriptionCount = user.get("subscriptionCount");,我们最终会得到过时的订阅计数。

以下是控制台输出示例:

I2015-02-17T23:10:14.972Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.022Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.073Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.145Z] 1
I2015-02-17T23:10:15.224Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":1}}
  Result: Update changed to {"followersCount":0,"followingCount":0,"subscriptionCount":1}
I2015-02-17T23:10:15.244Z] 1
I2015-02-17T23:10:15.257Z] 1
I2015-02-17T23:10:15.276Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":1}}
  Result: Update changed to {"followingCount":0,"subscriptionCount":1,"followersCount":0}
I2015-02-17T23:10:15.329Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":1}}
  Result: Update changed to {"subscriptionCount":1,"followersCount":0,"followingCount":0}
I2015-02-17T23:10:15.576Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.611Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: ...
I2015-02-17T23:10:15.685Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: ...
I2015-02-17T23:10:15.692Z] 2
I2015-02-17T23:10:15.706Z] 2
I2015-02-17T23:10:15.715Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":2}}
  Result: Update changed to {"subscriptionCount":2,"followersCount":0,"followingCount":0}
I2015-02-17T23:10:15.732Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":2}}
  Result: Update changed to {"followersCount":0,"followingCount":0,"subscriptionCount":2}
I2015-02-17T23:10:15.789Z] 3
I2015-02-17T23:10:15.817Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":3}}
  Result: Update changed to {"followersCount":0,"followingCount":0,"subscriptionCount":3}

同样的事情发生在afterDelete之后。知道如何解决这个问题吗?我能想到的另一个选择就是直接查询订阅类,但这是一种非常低效的方法。

1 个答案:

答案 0 :(得分:1)

我应该仔细阅读文档。此用例记录在此处:https://parse.com/docs/js_guide#objects-updating

只需将user.set("subscriptionCount", subscriptionCount + 1);替换为user.increment("subscriptionCount", 1);

即可

增量是原子操作。