This is not going to be the best parse.com question out there, but it's the first time I write something in javascript and I can't figure out what's wrong.
In my db I have ParsePosts, ParseUsers and ParseComments. When I delete a post, I want the following to be done:
- Task 1: find the user who wrote the post, and decrement his "posts" counter;
- Task 2: delete all
ParseComments that were related to the post to-be-deleted; - Task 3: find user who "liked" the post, and decrement their "likes" counter.
I'm trying as follows (renaming some variables so there might be typos):
Parse.Cloud.beforeDelete("ParsePost", function(request,response) {
var post = request.object;
var user = post.get("author");
user.increment("posts",-1);
user.save().then(function(success) {
var query = new Parse.Query("ParseComment");
query.equalTo("post",post);
return query.find();
}).then(function(comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
comment.destroy();
}
var query2 = new Parse.Query("ParseUser");
query2.equalTo("postsLiked",post); //"postsLiked" is a relation field
return query2.find();
}).then(function(users) {
for (var i = 0; i < users.length; i++) {
var u = users[i];
u.increment("postsLikedCount",-1);
u.save();
}
response.success();
});
});
However, deleting fails with the message: Result: success/error was not called. I have no idea why.
The way I dealt with promises, as far as I understood (not much), should call the next then() even if there's an error in one of the steps before. In other words, the three tasks are not really consequential - I'd like the function to:
- try to accomplish each task;
- call success() no matter the output of the tasks: I want the post to be deleted even if, say,
comments.length == 0.
How should I accomplish that, and why my function is not reaching response.success()?
As a side note, I haven't even found a way to debug - I use parse deploy and then look at the logs section in parse.com, but I can't manage to write anything into it. Namely, console.log() gives no output.