Delete _Session in afterDelete Cloud Code

Hello, I would like to achieve a behaviour where there will be all sessions deleted after a user decides to delete account in the client app. I am struggling to get the code right:

Parse.Cloud.afterDelete(Parse.User, (request) => {
  var objectId = request.object.id;
  console.log("The user " + objectId + " deleted");
  const query = new Parse.Query("_Session");
  query.equalTo("user", request.object.id);
  query.find({useMasterKey: true})
  .then(Parse.Object.destroyAll) 
    .catch((error) => {
      console.error("Error finding related session " + error.code + ": " + error.message);
    });
});

The User object gets deleted, but there is nothing happening with the _Session. Could you please aim me in a correct direction?

Thank you!

User is a pointer, so you would want the query param equalTo to also be a pointer (instead of request.object.id

Parse.Cloud.afterDelete(Parse.User, ({object, log}) => {
  const query = new Parse.Query(Parse.Session);
  query.equalTo("user", object);
  query.find({useMasterKey: true}).then((sessions) => {
    return Parse.Object.destroyAll(sessions, {useMasterKey: true});
  }).catch((error) => {
    log.info(`Error finding related session ${error.code}: ${error.message}`);
  });
});
2 Likes

Thank you! I was breaking my head over it for last 2 hours as I am only experienced in Swift and this is basically my first .js code. I see that I have a plenty to learn still.

I must say, I am impressed how active and fast response community is around Parse.

No worries! JS can be a bit confusing at first so feel free to ask questions. We’re always interested in looking at how we can improve our docs to help new-comers learn quicker - so feel to let us know what you find challenging.

For someone like me (mechanical engineer self-learning swift) is JS very unreadable, indeed. I’m looking through the cloud docs, but I can’t figure out why my further extension of your function does not work:

Parse.Cloud.afterDelete(Parse.User, ({object, log}) => {
  const query = new Parse.Query(Parse.Session);
  query.equalTo("user", object);
  query.find({useMasterKey: true}).then((sessions) => {
    return Parse.Object.destroyAll(sessions, {useMasterKey: true});
  }).catch((error) => {
    log.info(`Error finding related session ${error.code}: ${error.message}`);
  });

  //setting profile status to "deleted", before it gets cleaned after 30 days
  const profileQuery = new Parse.Query("PrsProfile");
  profileQuery.equalTo("objectId", object.id);
  profileQuery.first({useMasterKey: true}).then((profile) => {
    log.info(`to be marked as delete: ${profile}`);
    profile.set("stts", 3);
    return profile.save({useMasterKey: true});
  })
  .catch((error) => {
    console.error("Error while setting profile status to delete " + error.code + " : " + error.message);
  });
});

What I want to achieve is that after user deletes his account, his profile object changed a status field to different value, letting other know that was deleted. This is closest I could get, getting error that shows that it at least tries to write something:

Error while setting profile status to delete 119 : Permission denied for action addField on class PrsProfile.

there is already field “stts” in the class, so there might also be some wrong syntax. If you would spot anything wrong I would appreciate a little help!

Thank you!

Try this:

Parse.Cloud.afterDelete(Parse.User, async ({object, log}) => {
  const query = new Parse.Query(Parse.Session);
  query.equalTo("user", object);
  const sessions = await query.find({useMasterKey: true});
  
  Parse.Object.destroyAll(sessions, {useMasterKey: true});

  

  //setting profile status to "deleted", before it gets cleaned after 30 days
  const profileQuery = new Parse.Query("PrsProfile");
  profileQuery.equalTo("objectId", object.id);
  const profile = await profileQuery.first({useMasterKey: true});

  log.info(`to be marked as delete: ${profile}`);
  profile.set("stts", 3);
  return await profile.save(null,{useMasterKey: true});
});

First I refractored the code to use async/await syle. This is much more readable than chains for me.

Second ıf you return something, rest of the code wont run. So you have to use return at the end. I removed return from destroyAll

And third, when you save objects, masterkey is not first parameter, you should place null and then masterkey like this: profile.save(null,{useMasterKey: true});

Edit: Today is my cake day. Yay.

1 Like