0
votes

I've written up a Parse Cloud script in JS which gets called in my Objective-C app. When the script gets called I get an error with the code: 141. Here is the code, it gets called when a user accepts a friend request from another user (I'm also not entirely familiar with JS either):

Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) {

Parse.Cloud.useMasterKey();

var friendRequestId = request.params.friendRequest;
var query = new Parse.Query("FriendRequest");

//get the friend request object
query.get(friendRequestId, {

    success: function(friendRequest) {

        //get the user the request was from
        var fromUser = friendRequest.get("from");
        //get the user the request is to
        var toUser = friendRequest.get("to");

        var relation = fromUser.relation("friends");
        //add the user the request was to (the accepting user) to the fromUsers friends
        relation.add(toUser);

        //save the fromUser
        fromUser.save(null, {

            success: function() {

                //saved the user, now edit the request status and save it
                friendRequest.set("status", "accepted");
                friendRequest.save(null, {

                    success: function() {

                        response.success("saved relation and updated friendRequest");
                    }, 

                    error: function(error) {

                        response.error(error);
                    }

                });

            },

            error: function(error) {

             response.error(error);

            }

        });

    },

    error: function(error) {

        response.error(error);

    }

});

});

All help and suggestions are greatly appreciated. Thank you.

EDIT: In my code I am calling:

[PFCloud callFunctionInBackground:@"addFriendToFriendsRelation" withParameters:@{@"friendRequest" : friendRequest.objectId} block:^(id object, NSError *error) {

EDIT2: The error I am getting in the JS script is the last

        error: function(error) {

        response.error(error);

    }

to be called.

1
may be similar to stackoverflow.com/questions/22616799/… so make sure that your main.js file is up to date with this code and that you are calling the exact name addFriendToFriendsRelation from your front end. Any slight misspelling may cause the error. - harmonickey
@harmonickey Check my edit, cheers - CrackerBarrelKid55

1 Answers

0
votes

This will solve your problem , all you have to do is

add

useMasterKey: true

in the find method. change your code to this

Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) {

Parse.Cloud.useMasterKey();

var friendRequestId = request.params.friendRequest;
var query = new Parse.Query("FriendRequest");

//get the friend request object
query.get(friendRequestId, {

   useMasterKey: true, success: function(friendRequest) {

        //get the user the request was from
        var fromUser = friendRequest.get("from");
        //get the user the request is to
        var toUser = friendRequest.get("to");

        var relation = fromUser.relation("friends");
        //add the user the request was to (the accepting user) to the fromUsers friends
        relation.add(toUser);

        //save the fromUser
        fromUser.save(null, {

            success: function() {

                //saved the user, now edit the request status and save it
                friendRequest.set("status", "accepted");
                friendRequest.save(null, {

                    success: function() {

                        response.success("saved relation and updated friendRequest");
                    }, 

                    error: function(error) {

                        response.error(error);
                    }

                });

            },

            error: function(error) {

             response.error(error);

            }

        });

    },

    error: function(error) {

        response.error(error);

    }

});

});