Reputation: 15
Im trying to get the data from firebase using an array of indexes as condition.
I have an array of categories and i need to get all the items with the categories from array.One item can have more categories. I have tried something like this but foreach is not doing anything to collection. I understand that you cant pass the array to firebase so i need some other solution.
const getItemsByCategories = (categories: string[]) => {
const collection = db.collection("items");
categories.forEach(category => {
collection.where("category", "array-contains", category);
});
return collection.get().then(mapQuerySnapshot);
};
Upvotes: 0
Views: 490
Reputation: 317828
If the "category" field is a list, and you're looking for all the documents that contain any of the items in another list you have in memory, then you should use an array-contains-any query instead. You can pass the array directly:
const collection = db.collection("items")
const query = collection.where("category", "array-contains-any", categories);
query.get().then(...)
Upvotes: 1