Dr Jorge
Dr Jorge

Reputation: 363

Parse Cloud Code weird bug

I have created a parse cloud beforeSave trigger for the predefined class ParseInstallation in order to delete duplicate parse installations for the same user, kinda like what is on this gist. My deployed cloud code is the following:

Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
   Parse.Cloud.useMasterKey();
   var query = new Parse.Query(Parse.Installation);
   //PROBLEM: request.object.get("username") is undefined...
   query.equalTo("username", request.object.get("username"));
   query.first().then(function(duplicate) {
      if (typeof duplicate === "undefined") {
          console.log("Duplicate does not exist, New installation");
          response.success();
      } else {
          console.log("Duplicate exist..Trying to delete " + duplicate.id);
          duplicate.destroy().then(function(duplicate) {
              console.log("Successfully deleted duplicate");
              response.success();
          }, function() {
              console.log(error.code + " " + error.message);
              response.success();
          });

      }
   }, function(error) {
      console.warn(error.code + error.message);
      response.success();
   });
})

The problem, as it is commented inline on the code, is that the request.object.get("username") returns undefined. In fact, all the properties of request.object are undefined!

On my android client app I associate the ParseUser and also the username to the ParseInstallation object, as follows:

user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
    if (e == null) {
        ParseInstallation installation = ParseInstallation.getCurrentInstallation();
        installation.put("username", username);
        installation.put("user", user);
        installation.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if(e == null) {
                    Log.e(TAG, "Parse installation was created successfully!");
                } else {
                    Log.e(TAG, "An error occurred while saving Parse installation: " + e.toString());
                }
            }
        });

        //more code...

    } else {
        Log.e(TAG, "Signup error: " + e.getMessage());

        //more code...
    }
}
});

So, given the above, one would expect that, on the parse cloud, request.object.get("username") would return the actual user username and not undefined, correct??? (Btw, yes I have checked that the username variable on the android client code is not null nor empty!)

Why the hell request.object properties are all undefined???

How to get username from ParseInstallation on the cloud trigger then???

Also, did anyone experienced this problem from an IOS client app?

Upvotes: 2

Views: 277

Answers (1)

rob
rob

Reputation: 156

Retrieve the username through the request.user object:

var User = request.user;
console.log(User.get("username"));

//or skip the first step:
console.log(request.user.get("username"));

Note you can also retrieve these values by calling the attributes property of the Parse.Object (typically bad practice):

console.log(request.user.attributes.username);

Upvotes: 1

Related Questions