Reputation: 532
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 = "[email protected]"
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
My question is if the fields for email and name are not empty why "observeSingleEvent" method returns null? How can I fix this?
Upvotes: 0
Views: 848
Reputation: 9945
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)
}
Upvotes: 1