代码之家  ›  专栏  ›  技术社区  ›  Mike

如何从firestore文档中检索密钥(使用swift 4/ios)

  •  0
  • Mike  · 技术社区  · 8 年前

    在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身份验证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())
    

    我得到了[2mhuccccxxxxxxxxxk4wkiat33:1],这是一个参与聊天的成员的firebase身份验证代码,是一个布尔值。

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

    谢谢

    2 回复  |  直到 8 年前
        1
  •  1
  •   Daniel Dramond    8 年前

    我不确定它是否是精确的语法,但是 self.defaultStore?.colleection 可能是双E的拼写错误。也可能是您遗漏了一个步骤。您进入chatID,希望在不实际访问它的情况下输出成员内部的键。之后我会访问成员 .document(chatID)

        2
  •  0
  •   isuru    3 年前

    你可以得到它如下。

    获取价值 document.data()

    获取密钥 document.documentID