Ad

How Can I Tell If I Value Already Exists In Firebase Firestore?

- 1 answer

Database entry

I would like to receive a bool letting me know if my document has a Wasiyyah or not..

What I've tried: Firestore.firestore().collection(user!.uid).document(docID).value(forKey: "Wasiyyah")

Which only crashes every time, so there must be something I'm not understanding here.

Ad

Answer

There isn't any function to check if a field exists in a document. You'll have to fetch the document and check for it's existence:

let docRef = Firestore.firestore().collection(user!.uid).document(docID)

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let data = document.data()
        // Check if the field exists in data
    } else {
        print("Document does not exist")
    }
}
Ad
source: stackoverflow.com
Ad