Vah.Sah Vah.Sah - 1 month ago 48
Swift Question

Firebase reading returns null

In my app I'm trying to get user information from Firebase database, but it always returns null. Here is my code for reading current user information from database

func workWithDataBase(){
let email = "test@gmail.com"
let password = "test23"


FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
print(error)
return
} else {
print("SIGNED IN\n")

let uid = FIRAuth.auth()?.currentUser?.uid

FIRDatabase.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot)
}) {
(error) in
print(error.localizedDescription)
}
}
})
}


and here is the output and the screenshot of Firebase database
enter image description here

enter image description here

My question is if the fields for email and name are not empty why "observeSingleEvent" method returns null? How can I fix this?

Answer

Try This:-

 FIRDatabase.database().reference().child("user").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
        }) {(error) in
            print(error.localizedDescription)
        }