0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I try it but it is shown this error: Can not use keyword 'await' outside an async function
Can you show a more complete example? You might need to use .then if you're not in an async context, or change the code that calls this so that it can use await

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.