Parse.com云代码 - afterSave运行两次

时间:2014-11-14 12:09:21

标签: javascript parse-platform

我在parse.com上运行了一些afterSave代码,显示你要运行两次。

这是云代码:

Parse.Cloud.afterSave("Review", function(request) {
    var query = new Parse.Query("ReviewAverage");
    query.equalTo("vendor", request.object.get("vendor"));
    query.find({ 
        success: function(average) {
            if ( average.length === 1 ){
                average = average[0];
                average.increment( "numReviews" );
                var currentTotal = average.get("totalScore") + request.object.get("rating");
                var currentAverage = currentTotal / average.get("numReviews");
                average.set("totalScore",  currentTotal ) ;
                average.set("averageScore", currentAverage) ;
                average.save();
            } else {
                var average = new Parse.Object("ReviewAverage");
                average.set("vendor", request.object.get("vendor"));
                average.set("numReviews", 1);
                average.set("totalScore", request.object.get("rating"));
                average.set("averageScore", request.object.get("rating"));
                average.save();
            }
        },
        error: function(e){
            console.error("Error updating averages: " + e);
        }
    });
});

我有我的评论表,而且,而不是动态计算相同项目的评论平均值(因为解析不支持按类型功能分组)我有另一个表,只跟踪项目的运行平均值(由供应商主导。)

我注意到ReviewAverage列中给定供应商的numReviews与同一vendo的实际审核数量之间存在一些差异。

查看日志,我看到的内容如下:

I2014-11-14T08:08:45.818Z] v13: after_save triggered for Review for user dA73DS733H
  Input: {"author":{"className":"_User","objectId":"dA73DS733H","__type":"Pointer"},"createdAt":"2014-11-14T08:08:45.815Z","objectId":"j1wfWfQabv","rating":7,"review":"Lacking pigs in blankets and would have benefitted from more crunchy stuffing but otherwise good - the healthy Christmas option.","updatedAt":"2014-11-14T08:08:45.815Z","vendor":"Pret - Christmas veggie lunch","ACL":{"dA73DS733H":{"read":true,"write":true},"*":{"read":true}}}
  Result: Success
I2014-11-14T08:08:46.278Z] v13: after_save triggered for Review for user dA73DS733H
  Input: {"author":{"__type":"Pointer","className":"_User","objectId":"dA73DS733H"},"rating":7,"review":"Lacking pigs in blankets and would have benefitted from more crunchy stuffing but otherwise good - the healthy Christmas option.","vendor":"Pret - Christmas veggie lunch","createdAt":"2014-11-14T08:08:45.815Z","updatedAt":"2014-11-14T08:08:46.260Z","objectId":"j1wfWfQabv","ACL":{"*":{"read":true},"dA73DS733H":{"read":true,"write":true}}}
  Result: Success

我看到有些人遇到问题,因为他们在afterSave函数中重新保存 - 但我的保存只是对ReviewAverage对象的保存,所以不应该再次触发任何理由吗?

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

我也遇到过afterSave的问题,其中使用指向现有对象(类型Y)的指针创建一个新对象(类型X)会导致对象(类型Y)上的afterSave触发。

因此,如果我保存两个对象,似乎afterSave被触发两次。

您的ReviewAverage类中的任何列是否为Pointer类型?