Stone Preston
Stone Preston

Reputation: 1868

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

ive never used cloud code/javascript and I am trying to write some parse cloud code to find a user using a objectId passed in to the cloud function, and then update that users relation that holds friends and finally save that user.

below is the function im 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 piece this together using the Parse docs. but Im really not following it to well. Never used javascript and am finding the syntax confusing.

then im calling 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 was not called error. Though im calling response.success and response.error in the function so I dont know why that is? Can anyone lend a hand?

edit: after doing some more searching it looks like response.success and response.error should only be called once each, so I modified 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");
        }

    });


});

Upvotes: 5

Views: 763

Answers (1)

Arthur Cinader
Arthur Cinader

Reputation: 1612

An old question, but since it's been up-voted, maybe answering can help someone else :).

First off, there is an error in how you are saving fromUser.

fromUser.save({ success: ...

If you look at the api you can see that it should be of the form:

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

But the larger problem that kept you from finding your bug is that errors are getting eaten 'cause you are using the old style method of dealing with async code instead of using promises.

Below, I have re-written to use promises. Note:

  1. I always return promise generating calls (there are other options for catching errors in async code, but start with this.)
  2. Put a .catch at the end. The .catch is effectively the same things as .then(null, response.error) but either way, it is imperative that there is final backstop to catch errors. In your code above, the error was in a success block, that was running async, so when there was an error, it failed with no one to hear 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);
});

Upvotes: 0

Related Questions