[英]Swift - How do I check if a firestore document exists?
How do I check if a Google Firebase Firestore document exists? 如何检查Google Firebase Firestore文档是否存在?
I've tried the following code in my project: 我在项目中尝试了以下代码:
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")
}
}
}
Here is a picture of my data on firebase.google.com: Click here 这是我在firebase.google.com上的数据的图片: 单击此处
The print(UserName!) will return the correct value. print(UserName!)将返回正确的值。 The following code will output one and two but will stop there.
以下代码将输出一和二,但将在那里停止。
@JRLtechwriting solved my problem. @JRLtechwriting解决了我的问题。
"Use if let document = document, document.exists {. Check out the example here: https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document " “如果让document = document,document.exists {,则使用。在此处查看示例: https : //firebase.google.com/docs/firestore/query-data/get-data#get_a_document ”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.