如何检查 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!) 将返回正确的值。以下代码将输出 one 和 two 但会停在那里。
最佳答案
@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/