【问题标题】:Cloud Firestore unable to retrieve document or fieldCloud Firestore 无法检索文档或字段
【发布时间】:2020-08-07 06:35:40
【问题描述】:

我使用了 Firebase 文档提供的示例代码,它会打印出错误消息。我不知道问题是在代码中还是在数据库结构中,因为还有子集合。在这种情况下,我正在尝试检索“Home Title”字段,但是我听说这是不可能的(我可能错了),所以我试图检索“Sample Wedding”文档,但无济于事。这是我第一次使用 Swift 和 Firestore 编写项目。

这是我的代码:

    let db = Firestore.firestore()
    let docRef = db.collection("Weddings").document("Sample Wedding")

    docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        print("Document data: \(dataDescription)")
    } else {
        print("Document does not exist")
    }

这是我的数据库结构: ]

【问题讨论】:

  • 具体发生在哪里以及发生了什么错误?
  • 它打印出“文档不存在”,即使应该有一个。

标签: ios swift firebase google-cloud-firestore


【解决方案1】:

你可以这样试试

  let db = Firestore.firestore()
  db.collection("Weddings").document("Sample Wedding").getDocument { 
  (documentSnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            print("Document data: \(documentSnapshot)")
            if let title = documentSnapshot.get("Home Title") as? String {
            print(title)
           }
        }
    }

【讨论】:

  • 非常感谢!有用!你知道我将如何显示一个字符串数组的字段吗?
  • 如果让 title_array = documentSnapshot.get("Home Title") as? [字符串] { print(title_array) }
猜你喜欢
  • 2019-01-25
  • 2021-09-23
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2018-08-28
  • 2021-08-06
  • 1970-01-01
  • 2020-10-30
相关资源
最近更新 更多