Get the user data from firebase cloud-based database and convert into dictionary

165 views Asked by At

I am new to swift and I am trying to get the data from the firebase cloudbase database and trying to assign them in dictionary.

I have tried putting them on a dictionary and printing the values but it doesnot show anyerror message or print anything on the console.

func fetchUser(){
        let db = Firestore.firestore()
        db.collection("users").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
               for document in querySnapshot!.documents {
                if let dict = document as? Dictionary<String, Any> {
                    let email = dict["email"] as! String
                    print(email)
                }
               }
             }
         }
    }

I want to print all the email from the users collection and display on the console.

1

There are 1 answers

0
Hasratha Reddy On

I hope this will help you

for document in querySnapshot!.documents {
    let documentValue = document.data() // default dictionary
    print(documentValue["email"] as! String)
}