How do I check if a Google Firebase Firestore document exists?
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
The print(UserName!) will return the correct value. The following code will output one and two but will stop there.
if let document = document, document.exists {. Check out the example here: firebase.google.com/docs/firestore/query-data/…