ios - Swift - 如何检查 firestore 文档是否存在?

标签 ios swift firebase google-cloud-firestore

如何检查 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 但会停在那里。

最佳答案

@JRLtechwriting 解决了我的问题。

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

关于ios - Swift - 如何检查 firestore 文档是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49742381/

相关文章:

android - 从 Android 中的 Firebase 子项检索单个值

javascript - 从 Firebase 存储返回下载 URL 的函数

ios - 如何在 Swift 中创建一个圆形按钮?

ios - 如何使用 .p8 JWT key 测试推送? (APNs 授权 key )

ios - 如何使用 mapView 和 CLLocationManager 在 iOS Swift 中绘制两个位置之间的方向

ios - Swift - 方法链

ios - swift : Animate CALayer

ios - 应用内购买 - 收据验证 : status = 21004

ios - 使用 swift 在 iOS 中访问联系人详细信息

iOS Swift 3 捆绑通知