androidparse-platformparse-cloud-codeparse-android-sdkafter-save

using Parse.Could.AfterSave on adding a relation to a ParseObject


i'm just experiencing parse sdk and i wanted to use cloud code to increment a Number column whenever a relationship between two tables added by client(for example user likes a post) , in parse documentation there is just codes for incrementing a column based on saving an object and not a relation between tables using afterSave :

Parse.Cloud.afterSave("Comment", function(request) {
query = new Parse.Query("Post");
query.get(request.object.get("post").id, {
success: function(post) {
  post.increment("comments");
  post.save();
},
error: function(error) {
  console.error("Got an error " + error.code + " : " + error.message);
}
});
});

how can i use afterSave on changing relation between two tables? any help would be appreciated.


Solution

  • After some struggling with parse Documentation! finally i've solved my issue this way and it works just fine for counting relations between two tables for specific object, hope this saves others some time :

    Parse.Cloud.afterSave("SavedObject", function(request) {
      var likeQuery = request.object.relation("likers").query();
      likeQuery.count()(function(likersCount){
    
          request.object.set("likersCountColumn",likersCount);
          console.log("Likers Count Is : "+ likersCount);
          request.object.save();
      }); 
    });