Skip to content

Cloud Code: afterDelete trigger - cascade deletion of multiple objects not working. #3396

Closed

Description

@dnadri

Issue Description

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:

  1. Using {useMasterKey: true} in the appropriate places
  2. 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.

Environment Setup

  • Server
    • parse-server version: 2.3.2 (latest)
    • Operating System: iOS (client)
    • Hosted on Heroku

I appreciate any help. Thank you!

Activity

  1. flovilmart commented on Jan 19, 2017

    @flovilmart
    Contributor

    Can you post the logs of such operation when running with VERBOSE=1?

  2. dnadri commented on Jan 19, 2017

    @dnadri
    Author

    @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.

    2017-01-19T05:37:30.323Z - RESPONSE from [GET] /parse/classes/Comment: {
      "response": {
        "results": [
          {
            "objectId": "hFbPKM0Ahb",
            "message": "2!",
            "post": {
              "__type": "Pointer",
              "className": "Post",
              "objectId": "KHvqFzwZlb"
            },
            "user": {
              "__type": "Pointer",
              "className": "_User",
              "objectId": "DoBnptx6Er"
            },
            "createdAt": "2017-01-19T05:35:03.031Z",
            "updatedAt": "2017-01-19T05:35:03.031Z"
          },
          {
            "objectId": "QEmWyp7xC5",
            "message": "1",
            "user": {
              "__type": "Pointer",
              "className": "_User",
              "objectId": "BRWswSVz25"
            },
            "post": {
              "__type": "Pointer",
              "className": "Post",
              "objectId": "KHvqFzwZlb"
            },
            "createdAt": "2017-01-19T05:34:49.291Z",
            "updatedAt": "2017-01-19T05:34:49.291Z"
          }
        ]
      }
    }
    2017-01-19T05:37:30.319Z - REQUEST for [GET] /parse/classes/Post: {
      "where": {
        "location": {
          "$nearSphere": {
            "__type": "GeoPoint",
            "longitude": -73.74051282619861,
            "latitude": 40.81445923150791
          },
          "$maxDistance": 0.006315044963120137
        }
      },
      "include": "user",
      "order": "-createdAt",
      "limit": "1000"
    }
    2017-01-19T05:37:30.299Z - REQUEST for [GET] /parse/classes/Comment: {
      "where": {
        "post": {
          "__type": "Pointer",
          "className": "Post",
          "objectId": "KHvqFzwZlb"
        }
      }
    }
    2017-01-19T05:37:30.235Z - RESPONSE from [DELETE] /parse/classes/Post/KHvqFzwZlb: {
      "response": {}
    }
    2017-01-19T05:37:30.234Z - afterDelete triggered for Post for user BRWswSVz25:
      Input: {"message":"AfterDelete","likeCount":2,"commentCount":2,"location":{"__type":"GeoPoint","latitude":40.816224,"longitude":-73.739558},"user":{"__type":"Pointer","className":"_User","objectId":"BRWswSVz25"},"createdAt":"2017-01-19T05:34:44.921Z","updatedAt":"2017-01-19T05:35:03.154Z","objectId":"KHvqFzwZlb"}
    2017-01-19T05:37:30.212Z - Current client number : 0
    2017-01-19T05:37:30.212Z - ClassName: "Post" | ObjectId: undefined
    2017-01-19T05:37:30.212Z - <appID>afterDelete is triggered
    2017-01-19T05:37:30.211Z - Subscribe messsage "{\"currentParseObject\":{\"message\":\"AfterDelete\",\"likeCount\":2,\"commentCount\":2,\"location\":{\"__type\":\"GeoPoint\",\"latitude\":40.816224,\"longitude\":-73.739558},\"user\":{\"__type\":\"Pointer\",\"className\":\"_User\",\"objectId\":\"BRWswSVz25\"},\"createdAt\":\"2017-01-19T05:34:44.921Z\",\"updatedAt\":\"2017-01-19T05:35:03.154Z\",\"objectId\":\"KHvqFzwZlb\",\"__type\":\"Object\",\"className\":\"Post\"}}"
    2017-01-19T05:37:30.210Z - Raw request from cloud code current : {"message":"AfterDelete","likeCount":2,"commentCount":2,"location":{"__type":"GeoPoint","latitude":40.816224,"longitude":-73.739558},"user":{"__type":"Pointer","className":"_User","objectId":"BRWswSVz25"},"createdAt":"2017-01-19T05:34:44.921Z","updatedAt":"2017-01-19T05:35:03.154Z","objectId":"KHvqFzwZlb"} | original : undefined
    2017-01-19T05:37:30.201Z - REQUEST for [DELETE] /parse/classes/Post/KHvqFzwZlb: {}
    2017-01-19T05:37:22.475Z - RESPONSE from [GET] /parse/scriptlog?level=info&n=100: {
      "response": [
        {
          "method": "GET",
          "url": "/parse/scriptlog?level=info&n=100",
          "headers": {
            "host": "my-app-dev.herokuapp.com",
            "connection": "close",
            "origin": "https://myappdashboard.herokuapp.com",
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36",
            "content-type": "text/plain",
            "accept": "*/*",
            "referer": "https://myappdashboard.herokuapp.com/apps/myapp/logs/info",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "en-US,en;q=0.8,he;q=0.6",
            "x-request-id": "76b3a966-c6fb-4555-b023-777e9b18d3c8",
            "x-forwarded-for": "69.121.59.149",
            "x-forwarded-proto": "https",
            "x-forwarded-port": "443",
            "via": "1.1 vegur",
            "connect-time": "1",
            "x-request-start": "1484804242412",
            "total-route-time": "0",
            "content-length": "218"
          },
          "body": {},
          "level": "verbose",
          "message": "REQUEST for [GET] /parse/scriptlog?level=info&n=100: {}",
          "timestamp": "2017-01-19T05:37:22.462Z"
        },
        {
          "level": "info",
          "message": "Parse LiveQuery Server starts running",
          "timestamp": "2017-01-19T05:36:21.077Z"
        },
        {
          "level": "verbose",
          "message": "Support key pairs Map {}",
          "timestamp": "2017-01-19T05:36:21.056Z"
        }
      ]
    }
    2017-01-19T05:37:22.462Z - REQUEST for [GET] /parse/scriptlog?level=info&n=100: {}
    2017-01-19T05:36:21.077Z - Parse LiveQuery Server starts running
    2017-01-19T05:36:21.056Z - Support key pairs Map {}
    

    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.

  3. dnadri commented on Jan 22, 2017

    @dnadri
    Author

    @flovilmart Any ideas what the issue is here?

  4. dstarke commented on Feb 21, 2017

    @dstarke
    Contributor

    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.

  5. flovilmart commented on Feb 21, 2017

    @flovilmart
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions