Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Firebase: Collection group query and then get parent of sub collection

My data model:

A Trip has a sub collection Rides and references the trip with tripId

A ride has an experience: string which I'd like to filter on.

So I thought to query the SubCollection Rides

export const getTripsByExperience = async (experience: EXPERIENCES) => {
  const ridesQuery = query(
    collectionGroup(db, 'rides'),
    where('experience', '==', experience)
  );
  const querySnapshot = await getDocs(ridesQuery);
  let trips: Trip[] = [];
  querySnapshot.forEach(async (rideDoc) => {
    const ride = rideDoc.data();
    const tripSnapshot = await getDoc(doc(db, 'trips', ride.id));
    trips.push({ ...tripSnapshot.data(), id: tripSnapshot.id });
  });
  return trips
}

When I console.log this out right after trips.push trips.length gets incrementally bigger, but when the loop finishes, the function returns an empty array 🤯

What is the right approach here to get the trips filtered by rides.experience?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

forEach does not work with async, just convert it to the map instead.

const tripsPromises = querySnapshot.docs.map(async (rideDoc) => {
    const ride = rideDoc.data();
    const tripSnapshot = await getDoc(doc(db, 'trips', ride.id));
    return { ...tripSnapshot.data(), id: tripSnapshot.id };
});

const trips: Trip[] = await Promise.all(tripsPromises);

Here is a similar question with more details and examples in the answers: Using async/await with a forEach loop

Update: The documents can be accessed as an array via the "docs" property or enumerated using the forEach method as per https://firebase.google.com/docs/reference/node/firebase.firestore.QuerySnapshot

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!