Search code examples
javascriptiosswiftparse-platformparse-cloud-code

AfterSave on PFUser checking if on creation or update


I am having difficulty in distinguishing between if a PFUser is being updated or if it is being saved for the first time. I implemented the solution found on parse.com here. However, it is not working as explained in the answer found at the link. I always get a false regardless of if it is on the creation or an update. Here is the code that I have implemented.

Parse.Cloud.afterSave(Parse.User, function(request) {
     Parse.Cloud.useMasterKey();
     //this error log prints false regardless of if the user is signing up.
     // or having a key updated.
     console.error(request.object.existed());
     if (!request.object.existed()) {
         //This is on sign up, the user has not existed prior. 
         //This doesn't work. This block of code is always run.
     }
     else {
         //This is supposedly when the user is having a key updated.
         //Unfortunately, this code is never run regardless.
     }
 )}

This code does not behave as I expect it to because the request.object.existed() always returns false. I think this may have to do that this is saving a PFUser and not some generic PFObject. Is there something I am missing using request.object.existed() on a PFUser afterSave? Thanks.


Solution

  • This seems to be an unresolved Parse cloud bug

    Possible workaround is to check the difference between create and update times to see if your object is being updated or is being created.