I need to write beforeDelete function in parse cloud code, with more than one query The following code will delete all comments, but not images
note: Post object has 2 columns (Relation and Relation) Image and Comment are custom classes
e.g.
Parse.Cloud.beforeDelete("Post", function(request, response) {
// delete all its comments
const commentsQuery = new Parse.Query("Comment");
commentsQuery.equalTo("postId", request.object.get("objectId"));
commentsQuery.find({ useMasterKey: true })
.then(function(comments) {
return Parse.Object.destroyAll(comments,{useMasterKey: true});
})
.catch(function(error) {
response.error("Error finding comments line 91 " + error.code + ": " + error.message);
});
// delete all its images
const postQuery = new Parse.Query("Post");
postQuery.equalTo("postId", request.object.get("objectId"));
// postQuery.include("images");
postQuery.first({useMasterKey: true})
.then(function(aPost){
var images = aPost.relation("images");
images.query().find({useMasterKey: true}).then(function(foundImages){
return Parse.Object.destroyAll(foundImages,{useMasterKey: true});
}).catch(function(error){
response.error("Error finding images line 107 " + error.code + ": " + error.message);
});
})
.catch(function(error) {
console.error("Error finding related comments " + error.code + ": " + error.message);
response.error("Error finding post line 113 " + error.code + ": " + error.message);
});
response.success();
});
Edit
Parse Log
2018-03-19T00:33:50.491Z - beforeDelete failed for Post for user undefined:
Input: {"User":{"__type":"Pointer","className":"_User","objectId":"LZXSK7AwE7"},"Content":"","Title":"ios ui, coffee shop app","isPublic":true,"createdAt":"2017-11-23T06:37:55.564Z","updatedAt":"2018-03-19T00:32:30.180Z","views":2,"images":{"__type":"Relation","className":"Image"},"mainColors":{"__type":"Relation","className":"Color"},"Comments":{"__type":"Relation","className":"Comment"},"objectId":"3UDJCIXxGS"}
Error: {"code":141,"message":"Error finding images line 107 undefined: A promise was resolved even though it had already been resolved."}
How to fix this to succeed in destroying all its images and comments