简体   繁体   中英

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

数据库条目

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM