0

I am retrieving a certain document from the collection products and I wanted to compare it to another collection of categories

Specific document from Product collection:

enter image description here

Category collection:

There might be some documents that does not have options, they could be determined by the field value. If it has a string yes, then it does have options. Otherwise, it will be a no.

enter image description here enter image description here

I can already display the specific document from the product collection. I used map to display the products.

Inside that map, I wanted to have a conditional statement that will display all of the options if the current product's category matches with any of the document in the collection categories that has options in it.

For example, the current product is Shirt with a category of S-L. In the category collection, the S-L does have options.

And if the current product does not have any options, then it will just display "No options"

What I did:

 {product &&
              product.map((prod, index) => (
                <li>{prod.cat}</li>
                <li>{prod.prodName}</li>
                  //if the current product matches with any of the document in the collection category, then show the options, else show "no options"
                   {category &&
                  category.map((c, index) => (
                    <>
                      <li>{c.cat}</li>

                      {c.cat === prod.cat ? <>1</> : <>2</>}
                    </>
                  ))}
              ))}

How can I do this?

JS3
  • 1,381
  • 5
  • 24

1 Answers1

0

It looks like you have built a relation based schema and you would like to perform an outer join using conditional statements. The map function does not support conditional statements as its role is to transform entities. As shared that Firestore does not support join on collections as that is a document storage type of database.

To help you get started, the Java Script allows to create an if statement in the body of map function, for details please check

The alternative strategy for you would be to keep the data within the main structure (product) and then fetch all data by single request and present them accordingly or you may want is to write code to read every document in every collection that would need a comparison, and also perform that comparison with the documents in memory. Below I share some resources that might be helpful to understand how data should be structured in Firestore.

Priyashree Bhadra
  • 2,257
  • 1
  • 15
  • so with my current data structure, I could solve my problem with a JavaScript or restructure my products and category collection where the categories will be moved inside my products documents instead of a separate collection? – JS3 Mar 02 '22 at 11:54
  • You could try with the first approach, I am not sure if it would yield you the desired output. I have linked an example of it, refer it in case you need the syntax and structuring. In my opinion, the second option is a better choice in the long run. – Priyashree Bhadra Mar 02 '22 at 11:57
  • so if I'm going to add products, there should already be an option to add category and add options for the cateogy so it will be saved in one collection? – JS3 Mar 02 '22 at 12:19
  • You can create subcollections and documents. Go through the videos I have sent you. – Priyashree Bhadra Mar 02 '22 at 12:41