jefelewis
jefelewis

Reputation: 2049

Cloud Firestore Document IDs

I'm trying to get the document Ids from a /users collection. However, I don't see an id on the objects that are being returned. How do I get the document Ids for each document with my code listed below?

Pages:

  1. AllUsers (Where I map all of the user objects to User components)

  2. ViewUser (When the user component is clicked on, I want to use this document Id here)

All Users Saga:

let allUsersData = yield documentSnapshots.docs.map(document => document.data());

Possibilities?

Upvotes: 0

Views: 52

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

Each DocumentSnapshot (like in your documentSnapshots.docs) has id and data().

You're explicitly dropping the document IDs when you do this:

let allUsersData = yield documentSnapshots.docs.map(document => document.data());

To get both, stick to using DocumentSnapshot or (if you want to stick to plain old JSON objects) merge the id with the data:

documentSnapshots.docs.map(document => ({ id: document.id, ...document.data() }));

Upvotes: 2

Related Questions