0

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);
};

1 Answer 1

1

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

4 Comments

I have tried that before and i get "Argument of type '"array-contains-any"' is not assignable to parameter of type 'WhereFilterOp'". I see i shoud update firebase dependency but since Im new to firebase i dont know hot to update. Any suggestions?
Make sure you're using the latest version of the Firestore SDK.
Now i have a situation where i need to pass more than 10 ids and firebase has a limit on 10. Any ideas how to solve this?
If you have a new question, please post it separately and explain what the problem is.

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.