Reputation: 21
I am not able to manage correctly the sync, async, and promises part related to Firestore query. I provide you a simplified version of my scenario. I have different categories of items and I want to display all the category with all items related in a particular way. This is defined by the function Display, that accept as a parameter an array, that contains items belonging to a category.
categories=["ct1","ct2"]
....
async function getItems(category){
let items=[]
const snap = await db.collection("items").where("category","array-contains",category).get()
snap.forEach((doc=>trainers.push(doc.data())))
return items;
}
....
LOOP ON CATEGORIES
Display(getItems(category))
Now the problem is the part of handling Promise. getItems return a promise not an array. How can I solve the problem and passing the data retrieved from Firestore to display function?
Upvotes: 0
Views: 93
Reputation: 1262
You need to await
your getItems
call.
async/await
are just syntactic sugar for Promises, so marking a function as async
will cause it to return a Promise, which you need to await
:
const items = await getItems('ct1')
// items is now an array
Upvotes: 1