In my app a User
can request to join another Users
account. The pseudo code for this is as follows:
username
of the account we want to joinusername
exist in the databaseno
returnyes
create a new AccountRequest
objectAccountRequest
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.
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.