【问题标题】:Swift - How do I check if a firestore document exists?Swift - 如何检查 Firestore 文档是否存在?
【发布时间】:2018-04-09 22:05:28
【问题描述】:

如何检查 Google Firebase Firestore 文档是否存在?

我在我的项目中尝试了以下代码:

let db = Firestore.firestore()
let UserName = UserNameText.text
print(UserName!)
let userRef = db.collection("users").document(UserName!)
    print("one")
    userRef.getDocument { (document, error) in
        print("two")
        if let document = document {
            print("three")
            if document.exists{
                print("Document data: \(String(describing: document.data()))")
            } else {
                print("Document does not exist")
            }
        }
    }

这是我在 firebase.google.com 上的数据图片: Click here

print(UserName!) 将返回正确的值。以下代码将输出 onetwo,但会停在那里。

【问题讨论】:

标签: ios swift firebase google-cloud-firestore


【解决方案1】:

@JRLtechwriting 解决了我的问题。

“使用 if let document = document, document.exists {。在此处查看示例:https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document

【讨论】:

    猜你喜欢
    • 2021-07-22
    • 2018-06-21
    • 2021-11-25
    • 2020-10-05
    • 1970-01-01
    • 2019-01-09
    • 2021-12-29
    • 2021-11-09
    • 2019-04-19
    相关资源
    最近更新 更多