简体   繁体   English

解析云代码-afterSave

[英]Parse Cloud Code - afterSave

I'm writing an afterSave function for my "likes" class and would also like to write an afterSave function for my "post" class. 我正在写一个afterSave功能为我的“喜欢”级和还要写一个afterSave功能为我的“后”级。 In my "likes" function I query for the liked post and then increment the likes column in the post object, consequently saving the post object. 在我的“喜欢”函数中,我查询喜欢的帖子,然后增加帖子对象中的“喜欢”列,从而保存帖子对象。

What happens is when I like a post, the afterSave is called, queries for the post, increments it, saves it, and then the afterSave for my "post" class is called and a never ending sequence is created - "like" -> "post" -> "like" -> "post". 当我喜欢一个帖子时,会发生什么,当调用afterSave ,查询该帖子,对其进行递增,保存,然后afterSave我的“ post”类的afterSave并创建一个afterSave无休止的序列-“ like”-> “发布”->“喜欢”->“发布”。

// Increment Counter
Parse.Cloud.afterSave("likes", function(request) {
                  var like = request.object.get("like");

                  var query = new Parse.Query("post");
                  query.get(request.object.get("post").id, {
                            success: function(post) {
                                post.increment("likes", 1);
                            }
                            post.save();
                            response.success("Updated Count");
                            },
                            error: function(error) {
                            response.error("Got an error " + error.code + " : " + error.message);
                            }
                        });
            });


Parse.Cloud.afterSave("post", function(request) {
     // Gets called after post.save(); 
     // Only should be called after new object
});

I would like the "likes" function and "post" function to only be called if a new obect is created. 我希望仅在创建新对象后才调用“喜欢”功能和“发布”功能。 Not an update. 没有更新。 And I don't want to create a custom function, and make the client side pass excess data to the server. 而且我不想创建一个自定义函数,并使客户端将多余的数据传递给服务器。

You can use ParseObject.existed() Parse JavaScript API Docs. 您可以使用ParseObject.existed() 解析JavaScript API文档。

if(!request.object.existed()) . if(!request.object.existed())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM