Invalid Session Error Parse AfterSave on User in cloud code

China☆狼群 提交于 2019-12-11 22:40:02

问题


I have a question regarding an invalid session token that I am getting in an afterSave method on a user object. I literally run the same query in the beforeSave and the afterSave in Parse cloud code and the one in the before save works while the one in the afterSave does not. I was wondering if there is something that I am not understanding in regards to how Parse works in that I am getting an invalid session token in the afterSave method but no problems in the beforeSave method.

Here is the query...

  var AccessCode = Parse.Object.extend("AccessCode");
  var query =  new Parse.Query(AccessCode);
  query.equalTo("code", accessCode);
  query.notEqualTo("isUsed", true);
  query.find({
    success: function(results) {
        //do some things
    },
    error: function(error) {
        //display the error
    }
  })

回答1:


I solved this problem by changing the code a little bit...

    Parse.Cloud.useMasterKey();
    var query =  new Parse.Query("AccessCode");
    query.equalTo("code", accessCode);
    query.notEqualTo("isUsed", true);
    query.find({
        success: function(results) {
        //do some things
        },
        error: function(error) {
       //display the error
       }
    })

All works fine an dandy now, but I am still unsure why it all didn't work in the first place.




回答2:


For some reason, afterSave on a Parse User object requires:

Parse.Cloud.useMasterKey();

to be able to work.

Not sure why, but other have reported the same issues. I struggled with this for a little while, everything worked after just adding master key stuff at the start of the afterSave.



来源:https://stackoverflow.com/questions/33003348/invalid-session-error-parse-aftersave-on-user-in-cloud-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!