Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access subcollection in firebase firestore v9

I'm trying to access the firebase firestore documents inside subcollection (messages):

user > user.uid > messages > docRef.id > date: Date.now()
                                         text: userText
                  userEmail: user.email
                  userName: display.name

I used the following:

const snapRef2 = collection(db, "users/" + user.uid + "/messages")
onSnapshot(snapRef2, (snapshot) => {
    snapshot.forEach((doc) => {
        console.log(doc.data());
    })
})

But this method works only when user.uid is a string like: const snapRef2 = collection(db, "users/randomstring/messages")

How do I access the documents inside messages?

like image 539
Mitchel Avatar asked Oct 27 '25 05:10

Mitchel


1 Answers

const snapRef2 = collection(db, `users/${user.uid}/messages`) 

Make sure to use back ticks instead of quotation marks

like image 77
Strid3r21 Avatar answered Oct 28 '25 18:10

Strid3r21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!