Search code examples
swiftparse-platformparse-cloud-code

Parse aftersave cloud function returns a success result but doesn't execute success function


I'm having trouble getting my aftersave cloud function to increment a value in another data table. The Parse documentation is a bit confusing on this front.

I have a table called "CandidateVotes" that stores a row for each vote on a "candidate" - and when a new vote row is saved, I would like to increment the total votes count stored in a different table called "CategoryCandidates" which has a row for each "candidate".

So the relevant information:
- Votes table is called "CandidateVotes"
- Candidates table is called "CategoryCandidates"
- Total votes count column in CategoryCandidates table is called "votes"
- I'm initializing the "CategoryCandidates" row with a "votes" value of 0

Here's my Parse aftersave cloud function:

Parse.Cloud.afterSave("CandidateVotes", function(request) {
  query = new Parse.Query("CategoryCandidates");
  query.get(request.object.get("candidateID").objectID, {
    success: function(candidate) {
      candidate.increment("votes");
      candidate.save();
    },
    error: function(error) {
    }
  });
});

This is what I see in the cloud code log:

I2015-09-05T04:18:21.151Z]v15 after_save triggered for CandidateVotes for user 7YfU2ETvb3:
  Input: {"object":{"candidateID":{"__type":"Pointer","className":"CategoryCandidates","objectId":"XcfYPgijtn"},"createdAt":"2015-09-05T04:18:21.150Z","objectId":"sLySF6MvvQ","updatedAt":"2015-09-05T04:18:21.150Z","userID":{"__type":"Pointer","className":"_User","objectId":"7YfU2ETvb3"}}}
  Result: Success

BUT the "votes" column is not incrementing. It remains 0.

Any ideas would be very much appreciated!


Solution

  • The objectId property is just id in JavaScript.

    //replace
    request.object.get("candidateID").objectID
    //with this
    request.object.get("candidateID").id