Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

Make sure to use back ticks instead of quotation marks

about 4 years ago · Juan Pablo Isaza Report

0

The code in your answer works if you want to retrieve the messages for a specific user, identified by user.uid.

If you want to get the messages for all users, you can use a collection group query. Such a query reads from all collections with a specific name, like messages:

const snapRef2 = collectionGroup(db, "messages")
...

The rest of your code can stay the same.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!