1
votes

I'm a relatively new Swift programmer and am using Firebase for the first time so please excuse any misunderstandings I may have and my lack of knowledge about terminology.

I am attempting to retrieve data about a user that is stored in a database (email and username).

The code successfully finds the userID in the database. The userID is then used in order to navigate into the directory containing the username and email. It stores those values in snapshot.

For some reason, when snapshot is printed, it shows the userID but the contents of the directory (username and password) are shown as <null>. I am certain that the directory I am attempting to access and retrieve data from exists and is not empty (it contains a username and email). I wantsnapshot to store the username and email, but printing shows that it is not doing so correctly and I cannot figure out why.

here is my code block:

func checkIfUserIsLoggedIn() {
        if Auth.auth().currentUser?.uid == nil {
            perform(#selector(handleLogout), with: nil, afterDelay: 0)
        } else {

            let uid = Auth.auth().currentUser?.uid;
            Database.database().reference().child("Users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
                
                print(snapshot)
                
                if let dictionary = snapshot.value as?[String:AnyObject] {
                    self.userLabel.text = dictionary["name"] as? String
                }
                
                
            }, withCancel: nil)
        }
    }

and here is what is being printed to the console: Snap (ywU56lTAUhRpl3csQGI8W8WmQRf1) <null>

Here is the database entry I am attempting to reach and log to snapshot: I'm a new Stack Overflow user and don't have enough experience on the site to be allowed to embed images in posts, so this is the external link

Thanks for reading, any help would be much appreciated!!

3
In firebase, you have users in lowercase while in the code you are using "Users". Maybe that's the case. - rv7284
If my answer solved your question can you mark it as accepted? - erdos

3 Answers

2
votes

Your reference in Firebase is to "users", but you are using .child("Users") in your code. Make sure your lookup matches case to your node. I find it best to create a reference to that node and use it for writing to and reading from.

let usersRef = Database.Database().reference().child("users")


Snap (ywU56lTAUhRpl3csQGI8W8WmQRf1) <null> the portion in parenthesis refers to the end node of what you are trying to observe. In this case it refers to uid!.

1
votes
if u want to get username or email then you make first the model class for 
Example:-


class User: NSObject {
    var name: String?
    var email: String?
}

then user firebase methed observeSingleEvent

    FIRDatabase.database().reference().child("user").child(uid).observeSingleEvent(of: .value, with: { (snapShot) in
                if let dictionary = snapShot.value as? [String: Any]{
            //    self.navigationItem.title = dictionary["name"] as? String
                let user  = User()
                    user.setValuesForKeys(dictionary)
                self.setUpNavigationBarWithUser(user: user)
                }
            })`
0
votes

if it is not finding your asking values, you are asking wrong directory. check firebase db child name it must be exactly like in your code ("Users")