1

I am confused on how to update a user from cloud code. If someone can help me out with getting my mind right about this I'd appreciate it.

The cloud code I will use for this example is:

cloud code

Parse.Cloud.define("like", function(request, response) {
    var userID = request.params.userID;

    var User = Parse.User();
    user = new User ({ objectId: userID });

    user.increment("likes");

    Parse.Cloud.useMasterKey();
    user.save().then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error)
    });

});

called from iOS

[PFCloud callFunctionInBackground:@"like" withParameters:@{@"userID": self.selectedFriendID} block:^(id object, NSError *error) {

}];

Questions

user = new User ({ objectId: userID });

1) is the "new" in the code above creating a "brand new" user, or is it "grabbing" an existing user at the objectId so that it can be updated on user.save?

2) if the latter is correct, than is it possible to "grab" a user from a column other than the objectId column, and maybe instead grab from the userID column?

eg:

user = new User ({ userID : userID });
Brandon A
  • 8,153
  • 3
  • 42
  • 77
  • What I would do to retrieve the user in cloud code is query.get(request.params.userID.... I believe this returns the user object. I do not know if you can do this with other columns. There is lots of stuff in the cloud code docs about this, though. – Jacob Aug 06 '14 at 21:34

2 Answers2

3

This line:

user = new User ({ objectId: userID });

Creates an instance of an object with a known ID, it only works with objectId. Any changes you make to that object are persisted, but no other data is changed (e.g. you won't accidentally blank out the other columns).

If instead you wanted to get a user by email you would have to do a query:

var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('email', userEmail);
userQuery.first().then(function(user) {
    if (user) {
        // do something with the user here
        user.set('new_col', 'new text');
        user.save().then(function() {
            response.success();
        });
    } else {
        // no match found, handle it or do response.error();
    }
});

// the above code is async, don't put anything out here and expect it to work!
Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • thanks for your response! I think I am starting to see whats going on here. So, `userQuery.equalTo('email', userEmail);` is grabbing the specific user by the user's email, and `userQuery.first().then(function(user)` is accessing the first column of the user, which would be `objectId`. So, under the `if (user) {}` in your code I should be able to do like `theUser = new User ({objectId : userQuery.objectId });` and then do `theUser.set('new_col', "new text");` save that and then in my data browser under this user there would be a new column of type string with `new text`? Right? – Brandon A Aug 07 '14 at 14:53
  • The `first()` function is to grab the first row/record, much better when you only expect at most one row returned. You will have access to the full user object in the result and can directly start setting values and save it. – Timothy Walters Aug 08 '14 at 06:07
2

What I would do to retrieve the user in cloud code is query.get(request.params.userID.... I believe this returns the user object. I do not know if you can do this with other columns. There is lots of stuff in the cloud code docs about this, though. Here is my cloud code function for editing a user, if that helps:

Parse.Cloud.define("editUser", function(request, response) {
  //var GameScore = Parse.Object.extend("SchoolHappening");
  // Create a new instance of that class.
  //var gameScore = new GameScore();
Parse.Cloud.useMasterKey();

  var query = new Parse.Query(Parse.User);
query.get(request.params.myUser, {
  success: function(myUser) {
    // The object was retrieved successfully.
    myUser.set("cabinetPosition", request.params.myPosition);
    // Save the user.
      myUser.save(null, {
        success: function(myUser) {
          // The user was saved successfully.
          response.success("Successfully updated user.");
        },
        error: function(gameScore, error) {
          // The save failed.
          // error is a Parse.Error with an error code and description.
          response.error("Could not save changes to user.");
        }
      });

  },
  error: function(object, error) {
    // The object was not retrieved successfully.
    // error is a Parse.Error with an error code and description.
  }
});

});
Jacob
  • 2,338
  • 2
  • 22
  • 39