我在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的实际Reviews数量之间存在一些差异。
在日志中,我看到类似以下内容:
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对象,所以应该没有理由再次触发吗?
我想念什么吗?
最佳答案
我也有afterSave的问题,在其中使用指向现有对象(Y型)的指针创建新对象(X型)会导致触发对象(Y型)上的afterSave。
因此,如果我保存两个对象,似乎afterSave被触发了两次。
您在ReviewAverage类中的指针中是否有任何类型的列?