2

I am trying to save a user (pull some data from Facebook and add it to some columns, that have nothing to do with the Parse username) in Parse's beforeSave trigger in the cloud, but I'm getting the following error at my iOS app while registering:

Can't modify username in the before save trigger

It's pretty self explanatory, right? Just with the little fact that I don't modify the username anywhere, neither in a trigger, cloud function, or client-side in my iOS app. I don't even access or use it. The user is created successfully with my cloud-code added fields though. But sometimes I just get this error:

Validation Failed

Then, my user object doesn't seem to save. Both errors are occuring randomly, with my same test user. I've even searched the whole project for the word "username" to be sure that I'm not setting it somewhere either directly or indirectly. I am NOT saving it anywhere. Is this a Parse bug, or am I missing something obvious? Here is my full code:

Parse.Cloud.beforeSave(User, function(request, response) {
    var user = request.object;
    if(typeof user.get("nickname") == "undefined" || user.get("nickname") == null){
      var randomNick = "User " + Math.floor(Math.random() * 1000000);
      user.set("nickname", randomNick);
    }
    if(user.get("facebookId") == undefined){
      var token = user.get('authData')['facebook']['access_token'];
      Parse.Cloud.httpRequest({
        url: 'https://graph.facebook.com/v2.1/me?fields=id,gender,name&access_token=' + token,
        success: function(httpResponse) {
          var responseData = httpResponse.data;
          user.set("facebookId", responseData.id);
          user.set("realName", responseData.name);
          if(responseData.gender !== undefined){
            user.set("gender", responseData.gender);
          }
          var acl = new Parse.ACL();
          acl.setPublicReadAccess(false);
          user.setACL(acl);

          user.save(null, {
            useMasterKey:true,
            success: function(){response.success()},
            error: function(o,e){response.error("error: " + e)}
          });
        },
        error: function(httpResponse) {
          console.error('Request failed with response code ' + httpResponse.status);
        }
      });
    }else{
      response.success();
    }
});
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • Maybe try console logging the username at various stages in the code. See if it changes at some point. – DanielST Nov 13 '14 at 18:22
  • User caching on parse is a mess, at least in the iOS SDK. Def try loging the username to console, but also consider periodically refreshing the local Parse User object (assuming that concept exists in js). – emma ray Nov 13 '14 at 19:11

1 Answers1

1

The likely problem is that you are trying to save an object in the beforeSave trigger; you don't need to save it, you just need to modify it and then call response.success() to let Parse.com know to continue with the save operation. The error you are getting is probably due to the fact that it's trying to create the same user twice - although that's just a guess.

See https://parse.com/docs/cloud_code_guide?language=JavaScript#functions-onsave for more information.

Remove the user.save() call and just call response.success().

Seth
  • 6,514
  • 5
  • 49
  • 58
  • That was it. I don't know why I called save again in a trigger, it could even go into a recursive loop (which happened before). Thanks, changing `save` to `response.success` solved the problem. – Can Poyrazoğlu Nov 14 '14 at 19:38