Angela C
Angela C

Reputation: 15

Cloud Firestore unable to retrieve document or field

I've used the sample code provided by the Firebase Documentation and it prints out the error message. I have no clue if the issue is within the code or within the structure of the database, as there are also sub-collections. In this case, I am trying to retrieve the "Home Title" field, however I heard that that's not possible (I may be wrong), so I'm trying to retrieve the "Sample Wedding" document, to no avail. This is my very first time programming a project in Swift and also using Firestore.

Here's my code:

    let db = Firestore.firestore()
    let docRef = db.collection("Weddings").document("Sample Wedding")

    docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        print("Document data: \(dataDescription)")
    } else {
        print("Document does not exist")
    }

Here's my database structure: database structure]

Upvotes: 0

Views: 140

Answers (1)

Pramod Shukla
Pramod Shukla

Reputation: 235

You can try it like this

  let db = Firestore.firestore()
  db.collection("Weddings").document("Sample Wedding").getDocument { 
  (documentSnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            print("Document data: \(documentSnapshot)")
            if let title = documentSnapshot.get("Home Title") as? String {
            print(title)
           }
        }
    }

Upvotes: 1

Related Questions