I have a FB function to retrive subcollection from Firestore. I need to find and fetch data from a parent collection doc containing email and display name. The function awaits for snapshop of the subcollection but wouldn’t wait for snapshot when I’m looping through the parent collection. I’m not sure how to make it work using promises.
Could someone please advise how to make the function to wait until the loop is finished fetching the parent data?
exports.loadUpcomingBookings = functions
.region("europe-west2")
.https.onCall(async (data, context) => {
if (context.auth.uid == "someUid") {
let now = new Date();
let bookings = [];
const snap = await db
.collectionGroup("checkout_sessions")
.where("appointmentDate", ">=", now)
.get();
snap.forEach((doc) => {
let booking = doc.data();
const parentDocId = doc.ref.parent.parent.id;
db.collection("customers")
.doc(parentDocId)
.get()
.then((docRef) => {
booking.email = docRef.data().email;
booking.displayName = docRef.data().displayName;
bookings.push(booking);
});
});
return bookings;
}
});
René Němec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.