Sergio Bost
Sergio Bost

Reputation: 3209

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

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.

Upvotes: 0

Views: 872

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

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

Upvotes: 2

Related Questions