Home > Blockchain >  How can I tell if I value already exists in Firebase Firestore?
How can I tell if I value already exists in Firebase Firestore?

Time:02-20

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.

CodePudding user response:

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")
    }
}
  • Related