0

I have a Parse class Comment. Each Comment is submitted by a ParseUser and I wanna track in my parse user how many comments he has submitted, so I've created the following triggers in Parse Cloud Code:

/*
* -------------   AFTER SAVE
*/
Parse.Cloud.afterSave("Comment", function(request) {

  //if it's the first save
  if(!request.object.existed())  {

    //increment the User's comments
    query = new Parse.Query(Parse.User);
    query.get(request.object.get("createdBy").id, {
      success: function(user) {

        user.increment("comments");
        user.save();

      }, error: function(error) {
        console.log("Error searching the User");
      }
    });
  }
});

/*
* -------------   AFTER DELETE
*/
Parse.Cloud.afterDelete("Comment", function(request) {

  //decrement the User's comments
  console.log("The user who submitted the comment has the id: " + request.object.get("createdBy").id);
  query = new Parse.Query(Parse.User);
  query.get(request.object.get("createdBy").id, {
    success: function(user) {

      console.log("Retrieved the user. He has username: " + user.get("username"));

      user.increment("comments", -1);
      user.save();

    }, error: function(error) {
      console.log("Error searching the user - error afterDelete@Comment 2");
    }
  });

});

The problem is that the afterSave trigger works, but the afterDelete doesn't and I can't figure out why. I can retrieve the User because the two console.log show out which is the correct user, but I can't save it, after the increment(user, -1). Thanks in advance.

EDIT:
Here the User trigger:

/*
* -------------   BEFORE SAVE
*/
Parse.Cloud.beforeSave("User", function(request, response) {

  var comments = request.object.get("comments");
  if(comments == null || comments < 0)
    request.object.set("comments", 0);  

  var itemSaved = request.object.get("itemSaved");
  if(itemSaved == null || itemSaved < 0)
    request.object.set("itemSaved", 0);

  var username = request.object.get("username");
  if(username.length < 6 || username.length > 20)
    response.error("username must be longer than 6");
  else  {
    response.success();
  }

});

/*
* -------------   AFTER DELETE
*/
Parse.Cloud.afterDelete("User", function(request) {
  query = new Parse.Query("Comment");
  query.equalTo("createdBy", request.object.id);
  query.find({
    success: function(comments) {
      Parse.Object.destroyAll(comments, {
        success: function() {},
        error: function(error) {
          console.error("Error deleting related comments " + error.code + ": " + error.message);
        }
      });
    },
    error: function(error) {
      console.error("Error finding related comments " + error.code + ": " + error.message);
    }
  });
});
giacavicchioli
  • 334
  • 2
  • 14

1 Answers1

2

Per Parse's documentation, "If you want to use afterDelete for a predefined class in the Parse JavaScript SDK (e.g. Parse.User), you should not pass a String for the first argument. Instead, you should pass the class itself."

spitz
  • 103
  • 1
  • 7