I have never used the cloud / javascript code, and I am trying to write some parsing cloud code to find the user using the object passed to the cloud function, and then update the user relationship that contains friends and finally save that user.
below is the im function using:
Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) { Parse.Cloud.useMasterKey(); var fromUserObjectId = request.params.fromUserObjectId; var acceptingUser = request.params.user; var query = new Parse.Query(Parse.User);
I managed to put this together using Parse docs. but Im really not following him well. Javascript has never been used, and I find the syntax is confusing.
then im calls the function with
//fromUser is a PFUser object defined further up [PFCloud callFunctionInBackground:@"addFriendToFriendsRelation" withParameters:@{@"fromUserObjectId" : fromUser.objectId} block:^(id object, NSError *error) { }
however, whenever this function is called, I get a success / error that was not caused by the error. Although im calls response.success and response.error in the function, so I don't know why this is? Can someone lend a hand?
edit: after doing some search, it looks like response.success and response.error should be called only once, so I changed my function to look like this:
arse.Cloud.define("addFriendToFriendsRelation", function(request, response) { Parse.Cloud.useMasterKey(); var fromUserId = request.params.fromUserObjectId; console.log("fromUserId:"); console.log(fromUserId); var acceptingUser = request.params.user; console.log("acceptingUser:") console.log(acceptingUser); var query = new Parse.Query(Parse.User); query.get(fromUserId, { success: function(user) { console.log("found user:"); console.log(user); var fromUser = user; var relation = fromUser.relation("friends"); relation.add(acceptingUser); console.log("added accepting user to relation"); fromUser.save({ success: function() { response.success("successfully saved user") }, error: function() { response.error("error saving user"); } }); console.log("found a user"); }, error: function() { console.log("error finding user"); } }); });