0

I'm using Parse's Cloud Code to write a method that will return and array containing two sub arrays. With on array being an array of teams and the other being an array containing arrays of those team members. With the purpose of getting a list of teams and their team members. The arrays will be passed to an iOS front end to display.

My data is currently structured so that there is a Team object that can have multiple Users but a User can only have one team.

Here's my cloud function as it lives now:

var arrayOfUsers = [];
var arrayOfTeamMembers = [];
var arrayOfTeams = [];

Parse.Cloud.define("GetTopTeams", function(request, response) {
    var query = new Parse.Query("Teams");
    arrayOfTeams = [];
    arrayOfTeamMembers = [];
    fullArray = [];
    query.find().then(function(teams) {
        //returns a raw list of teams
        return teams;
    }).then(function(teams) {
        arrayOfUsers = [];
        teams.forEach(function(team) {
            //add team to subArray1
            arrayOfTeams.push(team);
            //searches for all users within the team
            var memberQuery = new Parse.Query(Parse.User);
            memberQuery.equalTo('team', team);           
            memberQuery.find().then(function(tMember) {
                //adds the team member to initial array
                arrayOfUsers.push(tMember);
            });
        }).then(function(){
            //pushes the finished array to the team member array
            return arrayOfTeamMembers.push(arrayOfUsers);
        }).then(function() {
            return arrayOfTeams;
        });
    }).then(function() {
        return response.success([arrayOfTeams, arrayOfTeamMembers]);
    });
});

I'm able to populate the arrayOfTeams but getting the arrayOfTeamMembers is proving to be difficult. Is there a better way to nest them? I tried promises but am unfamiliar with Javascript promises. I've found similar posts but none that address the issue I'm having. Any suggestions?

1 Answer 1

1

You could use Underscore. It is already available in Cloud Code. Using it you will need only one query. And you could write something like this:

var memberQuery = new Parse.Query(Parse.User);
memberQuery.exists("team");
memberQuery.include("team");
memberQuery.find().done(function(users){
    var usersGroupedByTeam = _.groupBy(users, function(user) {
        return user.get("team").id;
    });
    var arrayOfTeams = _.chain(users)
        .map(function(user) { return user.get("team"); })
        .uniq(function(team) { return team.id; })
        .value();
    response.success({
        arrayOfTeams: arrayOfTeams, 
        usersGroupedByTeam: usersGroupedByTeam
    });
}).fail(function(error){
    //error handle
    response.error(error.message);
});

I suggest you to return your result as an object, instead of an array. In your final result you will have the Teams (arrayOfTeams) and your users grouped by Team (usersGroupedByTeam).

And to retrieve specific team's users you will use usersGroupedByTeam["A_TEAM_ID"].

NOTE: remember that, by default, Parse.Query brings only 100 records. You could change it using Parse.limit, but don't forget to take this into account.

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

3 Comments

Thanks Oscar! The code seems to work except the teamId that gets return is null. The user.get("team") will return the relation but not the actual Team object. Any suggestions?
Dominic, when you use memberQuery.include("team") you say to Parse to bring the complete object (not only the relation, or the id). So this code should work, as far as you have a team for ALL users. Actually when we use memberQuery.exists("team"), we ensure that only the users with a team will be queried. So, no reason for the error.
I tested in my environment with a similar model, and it works. Maybe if you give me more information about where exactly the error happens, I can help you 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.