I’m trying to populate an array with names from all documents in a collection, and also populate another array with all the subcollection documents from those parent documents.
let data = []
let names = []
const suppliers = await db.collection('suppliers').get()
suppliers.forEach(async supplier => {
names.push({name: supplier.data().name, id: supplier.id })
const deliveries = await db.collection('suppliers').doc(supplier.id).collection('deliveries').get()
deliveries.forEach(delivery => {
data.push(delivery.data())
})
})
console.log(names) // Populated
console.log(data) // Empty
The problem is that it doesn’t wait for the inner loop to finish before executing the code outside. The names
array gets populated but the the data
array is empty. How can i make the nested loop finish before executing the outside code?