1

I have a function that returns all the products, if and only if a specific category is defined inside the object.

What I am trying to do is to pass an array parameter on the function with an array of categories names like [banana, apple, orange] and I want to get all the products that have these fruits as categories.

the problem is, I need to pass an array in that function.

const categories: string[] = ['apple', 'banana', 'orange'];

get_sugested_products_by_category(categories) {
   this.products_collection = this.afs.collection('products', ref =>
     ref.where(`category.${category}`, '==', true)
   );
   return this.products = this.products_collection.valueChanges();
}

And I was thinking if is possible to make a query like this bellow:

categories.forEach(category => {
  this.products_collection = this.afs.collection('products', ref =>
    ref.where(`category.${category}`, '==', true)
);
George C.
  • 6,574
  • 12
  • 55
  • 80
  • There is no way to such an OR or IN query in Firestore at the moment. See https://stackoverflow.com/questions/47018212/implementing-or-in-firestore-query-firebase-firestore. I have a feeling you'll want an inverted collection for this lookup, similar to what I do here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Nov 30 '17 at 04:52
  • 1
    Possible duplicate of [Implementing OR in firestore query - Firebase firestore](https://stackoverflow.com/questions/47018212/implementing-or-in-firestore-query-firebase-firestore) – Frank van Puffelen Nov 30 '17 at 05:07

0 Answers0