7

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:

1
  • What is your expected output, and can you also provide us screenshot of your Firestore collection data? Commented Dec 13, 2021 at 14:58

1 Answer 1

5

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.

3
  • Thanks, this worked! I was trying to parse some field where I could have just used the whole object as the input.
    – engineer-x
    Commented Dec 14, 2021 at 5:03
  • Frank van Puffelen, can't we get the data in just one call, for example getDoc(fetch_referenced_data=True)? Commented Jul 17, 2023 at 6: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. Commented Jul 17, 2023 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.