Firebase | Retrieving Data Error

I'm making an app with a login page, which I want it to retrieve a username of a user from a set of user data in realtime firebase, but when I run the code and sign in as an account it displays

Snap (n9fd2UynzZT6rLXRs4puqYahWUk2) < null >

Func Code:

Code Block
func checkIfUserSignedIn(){
if Auth.auth().currentUser?.uid == nil{
logout()
}else{
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot)
}, withCancel: nil)
}
}


JSON
Code Block
{
"Test" : {
"users" : {
"guest" : {
"email" : "guest@test.com",
"name" : "guest",
"uid" : "n9fd2UynzZT6rLXRs4puqYahWUk2"
},
"guest2" : {
"email" : "guest2@test.com",
"name" : "guest2",
"uid" : "r5WS623Cg7UL0UPSoq0S61sJj8Z2"
}
}
}






Answered by Claude31 in 657405022
What were you expecting ?

You get the first userId.
Is it what you request with observeSingleEvent ?

probably you have to change to observe.
If that works, don't forget to close the thread on this answer.

May read this:
https://stackoverflow.com/questions/46515454/swift-firebase-convert-observesingleevent-to-observe-childadded?rq=1

Note: that's more a Firebase question than a Swift.
So you should look in detail at Firebase API doc and possibly ask to Firebase forums directly.
Accepted Answer
What were you expecting ?

You get the first userId.
Is it what you request with observeSingleEvent ?

probably you have to change to observe.
If that works, don't forget to close the thread on this answer.

May read this:
https://stackoverflow.com/questions/46515454/swift-firebase-convert-observesingleevent-to-observe-childadded?rq=1

Note: that's more a Firebase question than a Swift.
So you should look in detail at Firebase API doc and possibly ask to Firebase forums directly.
The output Snap (n9fd2UynzZT6rLXRs4puqYahWUk2) < null > does not seem to be an error.

What do you get if you change print(snapshot) to print(snapshot.value)?
When I changed print(snapshot) to print(snapshot.value)

I got Optional <null> for the output

I got Optional <null> for the output

Thanks for showing the result.

I'm not good at Firebase, but your users does not have a child node matching child(uid!).
The result is representing there is no such data in your database.

You should better learn how to access hierarchical objects in Firebase.
As already noted, this is not a good place to learn the basics of third party library like Firebase.
Firebase | Retrieving Data Error
 
 
Q