【问题标题】:How do I retrieve a key from a firestore document (using swift 4/ios)如何从 Firestore 文档中检索密钥(使用 swift 4/ios)
【发布时间】:2017-11-25 22:33:49
【问题描述】:

在 firebase 数据库的 firebase 文档中,有关于扁平化数据库的信息,并以消息应用程序为例。

https://firebase.google.com/docs/database/web/structure-data

 // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

在“members”/“one”下,每个键都是参与聊天的成员的姓名。我很确定在 firebase 数据库中有一个 getKey 方法来获取每个键。

我已经以类似的方式设置了我的数据库(使用 firestore 而不是 firebase),但是每个聊天都是一个唯一标识符,并且文档包含聊天的成员,其中键是他们的 firebase auth id,例如。

var docsref: DocumentReference?
                    docsref = self.defaultStore?.colleection("chats").document(chatID)
                    docsref?.getDocument { (document, error) in

                        if let _ = document {

                            print("document!.data()")


                        } else {
                            print("Document does not exist")
                        }
                    }

当我输出时:

print(document!.data())

我得到 ["2mHuccccxxxxxxxxxxxxk4wkIAt33": 1] 这是参与聊天的成员之一的 Firebase 身份验证代码和一个布尔值。

我想拿到那把钥匙,但不知道怎么办。

谢谢。

【问题讨论】:

    标签: swift google-cloud-firestore


    【解决方案1】:

    我不确定这是否是确切的语法,但 self.defaultStore?.colleection 可能是双 E 的拼写错误。也可能是您错过了一个步骤。您正在进入 chatID 并且您希望在成员内部输出密钥而不实际访问它。我会在.document(chatID) 之后访问成员

    【讨论】:

      【解决方案2】:

      您可以通过以下方式获取。

      获取价值document.data()

      获取密钥document.documentID

      【讨论】:

        猜你喜欢
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-17
        • 2021-04-14
        • 1970-01-01
        • 2020-10-30
        • 2018-05-13
        相关资源
        最近更新 更多