I am trying out Cloud Code for the first time and I've run into some trouble. I think I have most of this right. I'm trying to add a user's "ID" to another user's object at an array called "likes". The code I'm using is...
Parse.Cloud.define("lifeLike", function(request, response) {
var userID = request.params.userID;
var selectedFriendID = request.params.selectedFriendID;
var query = new Parse.Query(Parse.User);
query.equalTo("userID", selectedFriendID);
query.find().then(function (users) {
Parse.Cloud.useMasterKey();
users.add("likes", userID);
users.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
}, function (error) {
response.error(error);
});
});
when I call it in iOS
[PFCloud callFunctionInBackground:@"like" withParameters:
@ {
@"userID": [PFUser currentUser][@"userID"],
@"selectedFriendID": self.selectedFriendID
}
block:^(id object, NSError *error) {
if (error) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[NSString stringWithFormat:@"Unable to like this user.\n%@", [error userInfo][@"error"]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Liked" message:@"You now like this user." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
i get this error
Error: TypeError: Object [object Object] has no method 'add'
This is my first time trying Cloud Code. Can anybody help me out with why this is happening?
.find() returns an array with the results which doesn't have a method add().If you're only searching for one user, try using .first() which returns one object.