I have a collections of users where each document ID is the uid and the documents have 2 fields: user_name and team_id.
I have another collection of teams where each document ID is randomly generated and the documents have 2 fields: team_name and team_id (the randomly generated id).
I am using react router and useParams to a team name to a component.
I would like to query the team collection to see which document in there has team_name equal to the team name that is passed. Then I would like to get the team_id from the document that has that team name and use it to query the user collection for which users have that team_id.
I know if I can get the team_id then I can do something like:
const queryRef = query(
collection(db, 'users'),
where('team_id', '==', team.id)
);
const [users] = useCollectionOnce(queryRef);
and then inside my return do something like:
{videos.docs.map((user) => {
const data = user.data();
const userName = data.user_name;
return (
// display userNames in some way
);
})}
I was wondering what the best way to get the team_id value from the team_name value or if there are any other better ways to get the list of users.
What you are currently doing is pretty straightforward way to handle this situation. First fetching team ID by team name and then querying users with that team ID.
One way you can remove first query is to use team ID itself in the URL so you can directly query users but then the URL won't be much user friendly. Additionally you can try to format your URL like how StackOverflow does. It has question ID (which is required) but also has title of the question so users can get some context of URL. The title however isn't necessary and URL works as long as the question ID is present.