0

I have following function. If I only use orderBy or where, it works and I receive the documents. If I try to combine both, I receive null. Is it not possible to combine it like I did?

 const {documents} = useCollection('users', ['name', '==', 'Tom'] ,['age', 'asc'])
export const useCollection = (collections, _q, _ordBy) => {
    const [documents, setDocuments] = useState(null)
    const [error, setError] = useState(null)
    const q = useRef(_q).current
    const ordBy = useRef(_ordBy).current

    useEffect(()=>{
        let ref = collection(db, collections)

        ref = query(ref, where(...q), orderBy(...ordBy))

        const unsubscribe = onSnapshot(ref, (snapshot)=>{
            let results = []
            snapshot.docs.forEach(doc => {
                results.push({...doc.data(), id: doc.id})
            })
            setDocuments(results)
            setError(null)
        }, (error) => {
            setError('could not fetch')
        })
        return () => unsubscribe()
    }, [collections, q, ordBy])
    return {documents, error}
}

I tried only orderBy and only where and it works. After combining it, I receive null.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Pascal Meger
  • 21
  • 1
  • 6

0 Answers0