1

I have a system where when the current user accepts a request my app adds a relation between the user that sent the request to the current user.

However I want to also have the current user added to the other user's relation so that it is not just the current user that has a relation.

My code for the current user works, but not for the other user.

    //i use this pattern to add the requester to a friends relation to the current user 
    var relation = PFUser.currentUser()!.relationForKey("Friends")
    relation.addObject(passenger as PFObject)
    PFUser.currentUser()!.saveInBackground()

When I try the same pattern for the passenger it does not add the relation

      var relation = passenger.relationForKey("Friends")
      relation.addObject(PFUser.currentUser()!.self as PFObject)
      passenger.saveInBackground()

Edit 1:

After a lot of research I found this answer that says I need cloud code. https://stackoverflow.com/a/23305056/3822504 Is this the only way or is there an alternative? Could someone provide a cloud code solution?

Edit 2:

I answered my own question. You will need to edit the cloud code based on your needs. The biggest thing that helped me was knowing how to query user class in cloud code and getting the current user in cloud code.

Community
  • 1
  • 1
kareem
  • 903
  • 1
  • 14
  • 36

2 Answers2

0

This is the only way, afaik. Follow this guide to set up your cloud code environment, if you haven't already: https://parse.com/apps/quickstart#cloud_code/unix

Then you should define a function (the code after set-up will contain a sample "hello world" function, so you can copy the definition from it). For relation modification you should refer to their JS api docs - https://parse.com/docs/js/api/symbols/Parse.Relation.html

(Note: In order for it to work, it should containt Parse.Cloud.useMasterKey() line before you try to modify users)

After your function is implemented and cloud code is deployed (parse deploy), use PFCloud.callFunctionInBackground in your mobile app.

alchemiss
  • 398
  • 4
  • 16
0

Okay so after a while I was able to get a solution for my scenario. This should work for simple relations when a user accepts another user.

First my swift code. It is important to note that I created a function that takes in a PFUser as a parameter. I did this to refactor a larger function. This user is the user we want to save the current user to in the cloud code.

func createRelation(otherUser: PFUser)
{

    let fromUser: PFUser = otherUser
    let params = NSMutableDictionary()
    params.setObject(fromUser.objectId!, forKey: "Friends")


    PFCloud.callFunctionInBackground("addFriendToFriendsRelation", withParameters: params as [NSObject : AnyObject] ) {
        (object:AnyObject?, error: NSError?) -> Void in

        //add the person who sent the request as a friend of the current user
        let friendsRelation: PFRelation = self.currentUser!.relationForKey("Friends")
        friendsRelation.addObject(fromUser)
        self.currentUser!.saveInBackgroundWithBlock({
            (succeeded: Bool, error: NSError?) -> Void in

            if succeeded {
                println("Relation added to the current user")


            } else {

                println("Handle error in adding relation to current user ")
            }
        })
    }

}

Now for the parse cloud code

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

    Parse.Cloud.useMasterKey();
    //get the friend requestId from params
    var friendRequestId = request.params.Friends;
    var query = new Parse.Query(Parse.User);

    //get the friend request object
    query.get(friendRequestId, {
        success: function(friendRequest) {

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

            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() {

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

                },

                error: function(error) {

                response.error(error);

                }

            });

        },

        error: function(error) {

            response.error(error);
        }

    });

});

Furthermore make sure you use the command parse deploy in the parse directory to update the main.js

kareem
  • 903
  • 1
  • 14
  • 36
  • I copied and pasted this code, but get the error: 2015-09-16 18:51:24.286 Gestern[6919:806406] [Error]: ReferenceError: parent is not defined at e.n.value (Parse.js:13:7510) at e.query.get.success (main.js:60:22) at e. (Parse.js:12:27827) at e.s (Parse.js:12:26759) at e.n.value (Parse.js:12:26178) at e.s (Parse.js:12:26887) at e.n.value (Parse.js:12:26178) at e. (Parse.js:12:26831) at e.s (Parse.js:12:26759) at Parse.js:12:27145 (Code: 141, Version: 1.7.5) What could be the problem? I have no experience with JavaScript... – Nick Podratz Sep 16 '15 at 16:59
  • @NickPodratz make sure you actually have parse cloud installed, also you need to realize how you schemed your data, and use the code accordingly , i have a very specific scenario, but i am passing the ids from swift that i need, one more thing to note is parse wont create the relation column for you, so in your user class you would need to add that – kareem Sep 16 '15 at 17:08
  • Thanks for the advice, I had already adapted it. I checked if everything is set up and after that learned some JavaScript in order to properly structure the code for my purpose. But after further research I came to the conclusion, that this seems to be a bug in the Parse SDK as another user reported a similar issue. https://groups.google.com/forum/#!topic/parse-developers/-wmVSquWrVI. – Nick Podratz Sep 16 '15 at 20:35