【问题标题】:How to get document ID from Firestore upon a snapshot如何在快照上从 Firestore 获取文档 ID
【发布时间】:2020-10-12 23:39:27
【问题描述】:

我正在尝试从 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?

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    这很容易。只是doc.id

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2020-12-16
      • 2023-03-19
      • 1970-01-01
      • 2020-12-27
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多