No puedo hacer que esto funcione. El código es el siguiente y el problema es que res.data() no se muestra en el objeto de docs.
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, }); };Entiendo por el comentario que el campo childId es de tipo Reference . El siguiente código, usando Promise.all() , debería funcionar (no probado):
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() }); });¡Esta respuesta de aquí funcionó!
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) });