1

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.

Manuel
  • 14,274
  • 6
  • 57
  • 130
Arash GM
  • 10,316
  • 6
  • 58
  • 76

2 Answers2

1

I suggest using beforeSave instead. Here is an example code I extracted from one of my projects.

Parse.Cloud.beforeSave(ClassConstants.Item.TYPE, function(request, response) {

    if(!request.object.isNew()) {
        // Retrieve the relationship information in json string format. 
        // RELATION_QUEUE here is simply a string "queue"
        var relQueueJsonStr = JSON.stringify(request.object.op(ClassConstants.Item.RELATION_QUEUE));

        if( relQueue !== undefined ) {
            var relQueue = JSON.parse(relQueueJsonStr);
            // Retrieve the operation being performed to this existing object.
            // The value will be "AddRelation" if a the relation "queue" is  
            // being added to this object
            var operation = relQueue.__op;
            if (operation == "AddRelation"){
                console.log("Relation queue is being added");
               // Do what you like here. In your case increment something

                response.success();
            } else {
            // Relation is being removed
                console.log("Relation queue is being removed");
                response.success();
            }
        } else {
            console.log("No queue relation.");
            response.success();
        }
    } else {
        response.success();
    }
});
Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
  • thanks for your answer,i'll take a look at your answer deeper soon , but can you tell me why on beforeSave? – Arash GM Aug 30 '15 at 11:25
  • To be honest, I cant remember exactly. I think it is because in afterSave() it just tells you the resulting object. In beforeSave() it tells you what are the changes to be made. For example, the code I just gave comes with a check for "AddRelation" which explicitly says that a relation is being added to the object. – Mark Pazon Aug 30 '15 at 11:29
  • i think i expressed my issue not good enough and i think (if i get your code well) your solution works when a relation created in code but i've already had that relations on my tables so i've tried another solution , which works in my case with just counting relations on adding one, and i think it's easier and cleaner. much appreciate your help. – Arash GM Aug 30 '15 at 17:40
1

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();
  }); 
});
Arash GM
  • 10,316
  • 6
  • 58
  • 76