5

I have a web app that gets data from Firestore v9. My instance of Firestore has a field of data type "reference".

When I query and get the reference object, I get the below when I console.log the result.

screenshot

const ref = collection(firestoreDb, "users", "a_user", "subscriptions")
const firebaseResponse = await getDocs(ref)

firebaseResponse.forEach(doc => {
    console.log(doc.data())
})

The output doesn't have the string which I could then use to place into another collection or doc function.

The field I'm using refers to this object:

screenshot

I expect the output to include price and product_name.

I found some similar questions but there not exactly what I was looking for:

engineer-x
  • 2,173
  • 2
  • 12
  • 25

1 Answers1

3

If you have a DocumentReference field in your data, that is precisely the type that you'll get back when reading that data from Firestore. You'll need to call get() on that DocumentReference to get the data for the referenced document.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks, this worked! I was trying to parse some field where I could have just used the whole object as the input. – engineer-x Dec 14 '21 at 05:03
  • Frank van Puffelen, can't we get the data in just one call, for example getDoc(fetch_referenced_data=True)? – Harsh Narwariya Jul 17 '23 at 06:42
  • Firestore document reads are shallow. There is no way in the API to request data from the referenced document. To load data from the referenced document will require an extra call. – Frank van Puffelen Jul 17 '23 at 13:53