Search code examples
androidparse-platformparse-cloud-code

Performing multiple deletes in Cloud Function


I'm trying to perform a delete call to an image the user have uploaded, the user shall also be able to delete their files. So I've made a button in my android code that calls a cloud function:

 Parse.Cloud.define("deleteFile1", function(request, response) {

  var fileName = request.params.image1;

  if (!fileName) {
    response.error("Filename is not defined");
  } else {
      Parse.Cloud.httpRequest({
        method: 'DELETE',
        url: 'https://api.parse.com/1/files/' + fileName,
        headers: {
          'X-Parse-Application-Id': '***',
          'X-Parse-Master-Key': '***'
        },
        success: function(httpResponse) {
          response.success(httpResponse.text);
        },
        error: function(httpResponse) {
          response.error("Request failed: " + httpResponse.text);
        }
      });
  }
});

Now this won't work obviously, I need to delete the object it's related to first. Atleast that's how I have come to understand the deleting process. If so, how do I perform first, a deleting of the file's object, then the file itself?

Can I run the object.remove("objectThatHoldsFile"); and do a saveCallBack. When the save have been performed, can I run the cloud function for deleting the file? Or must both parts be handled in the same cloud function in sequence?

Thanks in advance!


Solution

  •   Parse.Cloud.useMasterKey();
      var query = new Parse.Query("myClazz");
    
      query.get("xWMyZ4YEGZ").then(function(myClazz) {
         _class = myClazz;
         return _class.destroy()
       }
    ).then(function(fileName) {
          Parse.Cloud.httpRequest({
            method: 'DELETE',
            url: 'https://api.parse.com/1/files/' + fileName,
            headers: {
              'X-Parse-Application-Id': '***',
              'X-Parse-Master-Key': '***'
            } } ) } )
      .then(function(success) {
        // The related objClazz and file were deleted
      }, function(error) {
        console.error("Error deleting related comments " + error.code + ": " + error.message);
      });