【问题标题】:Firestore getDocument always returns document even if non existant, docs incomplete?Firestore getDocument 总是返回文档,即使不存在,文档不完整?
【发布时间】:2017-11-27 09:18:44
【问题描述】:

我开始使用 Firestore 并偶然发现了一个问题。

Firestore 文档中用于检索文档的代码是:

let docRef = db.collection("cities").document("SF")

docRef.getDocument { (document, error) in
    if let document = document {
        print("Document data: \(document.data())")
    } else {
        print("Document does not exist")
    }
}

但是,如果我没有具有该id的文档,“让文档=文档”将始终通过,它会尝试打印不存在的文档数据,从而导致错误。

现在,这很容易通过将 if 更改为:

if let document = document, document.exists {
   ...
}

这不也应该记录在案吗?还是我忽略了什么?

编辑:Link to docs

【问题讨论】:

    标签: swift firebase google-cloud-firestore


    【解决方案1】:

    我无法提供权威的答案,但根据我的经验,您链接的文档中的 Swift 和 Android 示例是错误的。返回的文档快照永远不会为空,应始终测试 exists

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2021-10-03
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多