When I have fetched places that we can see in screenshot below, I need get their data, but it's reference to data, I can get the data without additional request like place.ref.data()
? If I have 30 places, should I make 30 requests, really? I use react native and the ref object has type DocumentReference https://rnfirebase.io/docs/v3.1.x/firestore/reference/DocumentReference
Asked
Active
Viewed 6,210 times
6

Andrey Shostik
- 164
- 2
- 13
-
You'll need to load those references explicitly from your code. Also see https://stackoverflow.com/a/46570119/209103 – Frank van Puffelen Dec 21 '17 at 15:22
-
5I have exactly the same problem. I am looking for a "populate" kind of an option when I fetch the original document, so the the inner reference field comes pre populated. If anyone could point us in the right direction that would be really helpful! – Harshil Shah Dec 22 '17 at 15:21
1 Answers
0
You need to create your own populate method.
Here's one example I made where my collection eventSchedule
has references to events
, then I have the array of events and I want to get the events from each eventSchedule... That's how I did:
...
const docRef = db.collection("eventSchedule").doc(dayName);
try {
const doc = await docRef.get();
if (doc && doc.exists) {
const eventSchedule = doc.data();
if (eventSchedule.activeEvents) {
const activeEventsRefs = eventSchedule.activeEvents;
eventSchedule.activeEvents = [];
await activeEventsRefs.reduce(
async (promise: any, event: any) => {
await promise;
const childDoc = await event.get();
if (childDoc) {
eventSchedule.activeEvents.push(childDoc.data());
}
},
Promise.resolve()
);
}
} else {
logMessage += `No docs found for eventSchedule for today - ${dayName}`;
}
} catch (error) {
logMessage += `Error getting document ${JSON.stringify(error)}`;
}
So I used reducer to handle the promises. It works like a charm and it was the simplest "manual" way of doing this.
Also if you use React + Redux + Firebase, you can check the library react-redux-firebase that already implements a lot of stuff including the populate
that is, similar to the MongoDB populate and the SQL Join:
http://react-redux-firebase.com/docs/populate.html

Frederiko Ribeiro
- 1,844
- 1
- 18
- 30