0
  • Javascript
  • Parse Cloud Code

In my app a User can request to join another Users account. The pseudo code for this is as follows:

  1. send up the username of the account we want to join
  2. search to see if the username exist in the database
  3. if no return
  4. if yes create a new AccountRequest object
  5. add the newly created AccountRequest object to the user we were searching for.

I'm able to do steps 1-4 however I'm having trouble accomplishing #5.

Here is my code that I'm working with.

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

  console.log("-sendAccountAdditionRequest");

  // Create the query on the User class
  var query = new Parse.Query(Parse.User);

  // Set our parameters to search on
  query.equalTo("username", request.params.adminUsername);

  // Perform search
  query.find({

    // We found a matching user
    success: function(results) {

        Parse.Cloud.useMasterKey();

        var fetchedUser = results[0]

        console.log("--found user");
        console.log("--creating new AccountRequest");

        // Create a new instance of AccountRequest
        var AccountRequestClass = Parse.Object.extend("AccountRequest");
        var accountRequest = new AccountRequestClass();

        // Set the User it is related to. Our User class has a 1..n relationship to AccountRequest
        accountRequest.set("user", fetchedUser);

        // Set out other values for the AccountRequest
        accountRequest.set("phone", request.params.adminUsername);
        accountRequest.set("dateSent", Date.now());

        // Save the new AccountRequest
        accountRequest.save(null,{

            // Once the new AccountRequest has been saved we need to add it to our fetched User
            success:function(savedRequest) { 

                console.log("---adding AccountRequest to fetched User");

                //
                // === This is where stuff breaks
                //

                var requestRelation = fetchedUser.relation("accountRequest");

                // Now we need to add the new AccountRequest to the fetched User. The accountRequest property for a User is array...I'm not sure how I'm suppose to append a new item to that. I think I need to somehow cast results[0] to a User object? Maybe? 
                requestRelation.add(savedRequest);

                // We perform a save on the User now that the accountRequest has been added.
                fetchedUser.save(null, {

                    success:function(response) { 
                        console.log("----AccountRequest complete");
                        response.success("A request has been sent!");
                    },

                    error:function(error) {
                        // This is printing out: ParseUser { _objCount: 2, className: '_User', id: 'QjhQxWCFWs' }
                        console.log(error);
                        response.error(error);
                    }
                });


                //
                // ================================
                //


                //response.success("A request has been sent!");
            },

            // There was an error saving the new AccountRequest
            error:function(error) {
                response.error(error);
            }
        });
    },

    // We were not able to find an account with the supplied username
    error: function() {
      response.error("An account with that number does not exist. Please tell your administrator to sign up before you are added.");
    }
  });
});

I believe my problem is fetching the accountRequest relation from the returned search results from the initial query. Another thing to note is that I have not created the accountRequest property of my PFUser, my understanding is that this will automatically be done by Parse when I perform the save function.

4
  • Are you using the hosted Parse.com service, or the open source parse-server? Commented Jul 14, 2016 at 22:21
  • @MichaelHelvey open source Commented Jul 15, 2016 at 15:31
  • 1
    Just wondering because (although not entirely the topic of the question) just wanted to point out that in a node environment (parse-server) Parse.Cloud.useMasterKey(); won't do anything. If you want to use your masterKey, you have to explicitly use it in the options parameter of a save or a query. I.e. object.save(null, {useMasterKey: true}); Commented Jul 15, 2016 at 17:15
  • @MichaelHelvey got it. Thank you so much for the feedback! Commented Jul 18, 2016 at 14:03

1 Answer 1

1

Whether the accountRequest will get created for you depends on whether class level permissions are set to allow the client application to add fields. This is probably set to NO for your Parse.User.

But the relation on Parse.User isn't needed anyway, since you've already established it on the AccountRequest class. Given a user, you can get it's accountRequests with:

PFQuery *query = [PFQuery queryWithClassName:@"AccountRequest"];
[query whereKey:@"user" equalTo:aUser];
query.find()...

This is equivalent to getting the relation on User, getting its query and running it.

A couple notes about your code: (a) findOne will save you a line when you know there's just one result, (b) using Parse.Promise would really tidy things up.

Sign up to request clarification or add additional context in comments.

3 Comments

awesome man I appreciate it. Pretty new to the CloudCode side of Parse so trying to work my way through it. :)
I tried your suggestion for finding AccountRequest buy querying for the user property however it never returned any results. I added an ownerObjectId property to my AccountRequest class and I am able to successfully query on that. I posted a new question on the user relation query here if you have any suggestions: stackoverflow.com/questions/38484995/query-relation-in-parse
Sure. Will check it in about an hour. Should not need any new attributes, but I'll check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.