本文介绍了Parse.com云代码-afterSave运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在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对象,所以应该没有理由再次触发吗?

我想念什么吗?

解决方案

我也遇到了afterSave的问题,在其中使用指向现有对象(类型Y)的指针创建新对象(类型X)会导致afterSave on要触发的对象(类型Y).

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

您的ReviewAverage类中的指针是否为指针类型?

I have some afterSave code running on parse.com and it appears yo be running it twice.

Here is the cloud code:

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

I have my review table, and also, rather than calculating review averages for the same item on the fly (as parse doesnt support group by type functionality) I have another table that just keeps track of the running average for an item (keyed by vendor).

I have noticed some discrepancies between the numReviews for a given vendor in the ReviewAverage column against the actual number of Reviews that exist for the same vendo.

Looking in the logs, I am seeing stuff like the following:

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

I have seen some people having issues, because they are re-saving in the afterSave function - but my save is only ever to the ReviewAverage object, so shouldn't be any reason to trigger again?

Am I missing something?

解决方案

I'm having issues with afterSave too, where creating a new object(type X) with a pointer to an existing object(type Y) is causing afterSave on object(type Y) to trigger.

So if I save both objects it seems like afterSave was triggered twice.

Are any of the columns in your ReviewAverage class of type Pointer?

这篇关于Parse.com云代码-afterSave运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:53