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();
}
});