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

151
Views
How to get reference data inside map of parent data in Firebase

I can't get this to work. Code is as follows and problem is res.data() doesn't show in de docs object.

getProjects = async () => {
    const colRef = db.collection('parentCollection').orderBy('createdAt', 'desc');
    const snapshots = await colRef.get();
    const docs = snapshots.docs.map((doc) => ({
      docId: doc.id,
      ...doc.data(),
      ...doc
        .data()
        .childId.get()
        .then((res) => res.data()),
    }));
    this.setState({
      list: docs,
    });
  };

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

0

I understand from the comment that the childId field is of type Reference. The following code, using Promise.all(), should do the trick (untested):

const colRef = db.collection('parentCollection').orderBy('createdAt', 'desc');
const snapshots = await colRef.get();

const promises = [];
snapshots.forEach(snapshot => {
    promises.push(db.doc(snapshot.data().childId).get())
})

const childrenSnapshots = await Promise.all(promises); // Returns an Array of Document Snaphots, in the same order than the parents

const docs = [];
snapshots.forEach((snapshot, index) => {
    docs.push({
        docId: snapshot.id,
        ...snapshot.data(),
        ...childrenSnapshots[index].data()
    });
}); 
about 4 years ago · Juan Pablo Isaza Report

0

This answer from here worked!

db.collection('parentCollection').get()
    .then(res => {
      vm.mainListItems = [];
      res.forEach(doc => {
        let newItem = doc.data();
        newItem.id = doc.id;
        if (newItem. childId) {
          newItem. childId.get()
          .then(res => { 
            newItem.userData = res.data() 
            vm.mainListItems.push(newItem);
          })
          .catch(err => console.error(err));
        } else {
          vm.mainListItems.push(newItem);  
        }

      });
    })
    .catch(err => { console.error(err) });
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!