Vah.Sah
Vah.Sah

Reputation: 532

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 = "[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 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?

Upvotes: 0

Views: 848

Answers (1)

Dravidian
Dravidian

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

Related Questions