0
votes

I am trying to get the documentId from a Firestore database.

My Firestore database looks like this:

- chatMessages
  - chatId
    - messageId // <-- I want to get this
      - Object: { text, user, sentAt }

I am retrieving the data as follows, which works, but am unable to figure out how to get the documentId:

firebase.firestore().collection("chatMessages").doc("chatMessages").collection(chatId).get()
.then((snapshot) => {
  if (!snapshot.empty) {
    const data = snapshot.docs.map((doc) => {
      // const id = doc.getId(); // <-- how can I get messageId here?
      // return id;
    });
  }
})

However, I can't figure out how to get the id (messageId) for each document. I have tried the following, none of which have worked:

doc.getId()

doc.documentId

doc.documentID

How can I get the document ID of the documents I am returning as shown above?

1

1 Answers

0
votes

This is pretty easy. It's just doc.id