I feel like it was easier to get subcollection in v8 ,It’s been like 2 days trying to do it the new way but I gave up.
Im building a simple react social media app for learning purposes. where each user logs in and be able to post some text (and images but not atm), I have a main collection for Users and it has the users ID .each of these users have a collection called Posts and it contains all the user posts.
I can do so by entering the UID of each user like so
so what can i do to access the Users collection then get ALL the users and be able to access the Posts subcollection?
ps : sorry if any part of this question is unclear ,english isn’t my first language and it’s my first time posting here. appreciate any help!.
Advertisement
Answer
If you want to fetch posts from all the users, you are looking for collectionGroup
queries using which you can fetch documents in all the sub-collections named as ‘posts’. You can run a collectionGroup
query using Modular SDK (V9) as shown below:
import { getFirestore, getDocs, collectionGroup } from "firebase/firestore" const db = getFirestore() const allPosts = await getDocs(collectionGroup(db, "posts"))