Parse.com Cloud code Error: success / error was not called when trying to update the user

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); // find the user the request was from using the objectId query.get(fromUserObjectId, { success: function(user) { var fromUser = user var relation = fromUser.relation("friends"); relation.add(acceptingUser); fromUser.save({ success: function() { response.success("Successfully saved the users relation") }, error: function() { response.error("Save failed"); } }); }, error: function() { response.error("Save failed"); } }); }); 

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"); } }); }); 
+5
source share
1 answer

Old question, but since it was voted, maybe the answer might help someone else :).

Firstly, there is an error in how you save fromUser.

fromUser.save({ success: ...

If you look at the api , you will see that it should have the form:

fromUser.save(null, { success: ...

But the big problem that prevented you from finding your error is that errors get caught because you use the old-style method to work with asynchronous code instead of using promises.

Below I rewrote to use promises. Note:

  • I always return promising calls (there are other options for detecting errors in asynchronous code, but start with that.)
  • Put a .catch at the end. .catch are actually the same things as .then(null, response.error) , but in any case, you need to have error blocking. In the above code, the error was in the success block that async executed, so when the error occurred, it failed and no one heard it :).

 Parse.Cloud.define("addFriendToFriendsRelation", (request, response) => { const fromUserId = request.params.fromUserObjectId; console.log("fromUserId:", fromUserId); const acceptingUser = request.user; console.log("acceptingUser:", acceptingUser) new Parse.Query(Parse.User); .get(fromUserId, { useMasterKey: true }) .then((fromUser) => { console.log("found fromUser:", fromUser); const relation = fromUser.relation("friends"); relation.add(acceptingUser); console.log("added accepting user to relation"); return fromUser.save(null, { useMasterKey: true }) }) .then(response.success) .catch(response.error); }); 
0
source

Source: https://habr.com/ru/post/1201199/


All Articles