Home > Net >  Firebase 9: List subcollections of a document in React
Firebase 9: List subcollections of a document in React

Time:11-24

I'm trying to retrieve data from customers generated from stripe/firebase integration using the new Firebase 9 (web client side, React.js)

Documents look like this:

/customers/Eaoxxxxxxxxxxxxxxxxxx42/subscriptions/sub_1JylxxxxxxxxxxxxRd

The classic query doesn't "see" the subcollections:

React.useEffect(() => {
    const fetchData = async () => {
        const q = doc(db, "customers", currentUser.uid)
        const data = await getDoc(q)
        setCustomer(data.data());
    }
    fetchData()
}, [])

Ideas?

CodePudding user response:

There is no client-side API to list (sub)collections. To access documents, you'll need to know what (sub)collection they are in.

In the structure you show, the collection names are customers and subscription, both of which are known ahead of time and which you're expect to name in your code.


If you're looking to read subscriptions for a specific customer, you can access the subscriptions subcollection with:

const userSubscriptions = `collection(db, "customers", currentUser.uid, "subscriptions")`

If you want to read/query subscriptions across all users, use a collection group query which allows you to read from collections named subscriptions across the entire database.

  • Related