0

I'm trying to create an application where you have multiple blogs about lost, found and up for adoption pets. You can filter this blogs by:

'Categories (Lost, found, up for adoption)', 'Animal type (Dog, cat, bird, rabbit, etc)', 'Hair type (no hair, short, medium, long)', 'Gender (Female, male)', 'Eyes type (...)', 'HasCollar(yes, no)'

I've done a working filter but it was on the client side and since I'm going to handle a large amount of data, that solution won't work. I've also tried to solve this problem using queries provided by firestore but I've come across with the limitation where I couldn't compare more than two categories. This was the code

    if (activeCategory !== 'All' && activeGender === 'All' && activeAnimal === 'All') {
    q = query(
        collection(db, "blogs"),
        where("category", "==", activeCategory),
        where("gender", "!=", activeGender),
        where("animal", "!=", activeAnimal),
        )

    onSnapshot(
        q,
        async (snapshot) => {
            
         let list = []

    snapshot.docs.forEach((doc) => {
        list.push({id: doc.id, ...doc.data()})
    })

 lastDoc = await snapshot.docs[snapshot.docs.length - 1]
 firstDoc = await snapshot.docs[0]


    list.sort((a, b) => (a.timestamp < b.timestamp) ? 1 : -1)

    setBlogs(list)
    setFilters(list)
    setLoading(false)
    setActive("home")
    setLastDoc(lastDoc)
    setFirstDoc(firstDoc)
        }, (error) => {
            console.log(error)
        }

    ) 
    
} else if{...} }

useEffect(() => {
  fetchData()
}, [activeCategory, activeGender, activeAnimal])

So my only option is to use Firebase functions. I've talked to a friend and he told me that I needed to have endpoints like this, for example

https/firebase.adoptar.com/api/filter? activeCategory=adopcion 

The thing is, I don't know how to do it or where to search information in order to learn about this. It has an structure resembling node js, right? So I'm trying to come up with a solution using firebase functions. Can someone help me or tell me where to search so I can learn how to do it? Or how a query should look like in order to multifilter and update the page's info according the filter?

I've read firebase functions documentation but at best it tells you how to do a CRUD operation, nothing about filtering or creating an endpoint like the one above.

2
  • Firestore also have orderBy() that you can use to sort the results and limit() so you can paginate your data. Have you checked the documentation
    – Dharmaraj
    Commented Sep 20, 2022 at 3:49
  • I have read all the documentation and I tried using them but I'm faced with the limitation. I need to use functions Commented Sep 20, 2022 at 13:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.