You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cloud Code: afterDelete trigger - cascade deletion of multiple objects not working. #3396
Problem:
Using Parse Cloud Code's afterDelete trigger to cascade-delete multiple objects from different Parse classes. For example, after a user deletes a post, I want to also delete comments AND likes associated with that post.
Note: In Parse, I have a Post, Comment, and Like class.
I think the issue(s) with my code is related to either or both of the following:
Using {useMasterKey: true} in the appropriate places
Passing the associated comments results to the associated likes results in the following then promise block so to delete them together via destroyAll()
I've searched the existing Parse-server Github issues and tried StackOverflow, forums, but haven't found anything to help resolve this.
Steps to reproduce
Here's the code I've written (not working):
Parse.Cloud.afterDelete("Post", function(request) {
var objectsToDelete = [];
var commentQuery = new Parse.Query("Comment");
commentQuery.equalTo("post", request.object);
commentQuery.find({useMasterKey: true}).then(function(comments) {
var likeQuery = new Parse.Query("Like");
likeQuery.equalTo("post", request.object);
objectsToDelete.push(comments);
return likeQuery.find({useMasterKey: true});
}).then(function(likes) {
objectsToDelete.push(likes);
return Parse.Object.destroyAll(objectsToDelete, {useMasterKey: true});
}).then(function(success) {
console.log("successfully deleted all associated comments AND likes.");
}, function(error) {
console.error("Error deleting associated comments and likes " + error.code + ": " + error.message);
});
});
Expected Results
After a user deletes a post, it should also delete the comments AND likes associated with that post.
Actual Outcome
None of the comments or likes associated with the post were deleted.
@flovilmart Not sure if this is what you mean, but I've added the following to my ParseServer configuration in index.js:
var api = new ParseServer({
...
verbose: true,
...
});
Also, from my research, Parse Dashboard already provides the logs. Here are the logs I copied from the Parse Dashboard after deleting a post which had likes and comments associated with that post.
I personally can't tell much from these logs. So I'm not sure if there's a server-side issue or a problem with my Cloud Code syntax for achieving the expected results.
I think the problem is that the objectsToDelete array needs to be an array of Parse objects, but you are giving it an array of arrays of Parse Objects.
Try replacing objectsToDelete.push(<list>) with objectsToDelete = objectsToDelete.concat(<list>) both places where that pattern occurs.
I believe @dstarke has the answer. Also, note that this is not an issue with parse-server but with your core. For such questions, please use Stackoverflow instead.
Activity
flovilmart commentedon Jan 19, 2017
Can you post the logs of such operation when running with VERBOSE=1?
dnadri commentedon Jan 19, 2017
@flovilmart Not sure if this is what you mean, but I've added the following to my ParseServer configuration in
index.js:Also, from my research, Parse Dashboard already provides the logs. Here are the logs I copied from the Parse Dashboard after deleting a post which had likes and comments associated with that post.
I personally can't tell much from these logs. So I'm not sure if there's a server-side issue or a problem with my Cloud Code syntax for achieving the expected results.
dnadri commentedon Jan 22, 2017
@flovilmart Any ideas what the issue is here?
dstarke commentedon Feb 21, 2017
I think the problem is that the
objectsToDeletearray needs to be an array of Parse objects, but you are giving it an array of arrays of Parse Objects.Try replacing
objectsToDelete.push(<list>)withobjectsToDelete = objectsToDelete.concat(<list>)both places where that pattern occurs.flovilmart commentedon Feb 21, 2017
I believe @dstarke has the answer. Also, note that this is not an issue with parse-server but with your core. For such questions, please use Stackoverflow instead.