Reputation: 93
What Im trying to do is make 2 separate pages one with posts only form current user and the second one from all users. Can not figure out how to do the second one, how to select all users posts.
My firebase db tree looks like this - /users/*USERID*/link/*LINKID*
. On my page im rendering posts from current user that is logged in by:
const linksRef = db.collection(`users/${user.uid}/link`);
useEffect(() => {
linksRef.orderBy("timestamp", "desc").onSnapshot((snapshot) =>
setLinks(
snapshot.docs.map((doc) => ({
id: doc.id,
data: doc.data(),
}))
)
);
}, []);
Is there a way to do something like users/ALL/link?
or how can I handle this kind of stuff. Thank you for all responeses.
Upvotes: 0
Views: 93
Reputation: 50830
Try using collectionGroup
to get posts (or links) of all users (assuming links is a sub-collection in all users' documents):
To get all users' links:
const linksRef = db.collectionGroup("links").get().then((snapshot) => {
setLinks(
snapshot.docs.map((doc) => ({
id: doc.id,
data: doc.data(),
})
)
)
});
Just make sure there is a sub-collection "posts" in each user's document.
Upvotes: 1