簡體   English   中英

如何在快照上從 Firestore 獲取文檔 ID

[英]How to get document ID from Firestore upon a snapshot

我正在嘗試從 Firestore 數據庫中獲取 documentId。

我的 Firestore 數據庫如下所示:

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

我正在按如下方式檢索數據,它有效,但無法弄清楚如何獲取 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;
    });
  }
})

但是,我不知道如何獲取每個文檔的 id (messageId)。 我嘗試了以下方法,但都沒有奏效:

doc.getId()

doc.documentId

doc.documentID

如上所示,如何獲取我要退回的文件的文件 ID?

這很容易。 這只是doc.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM