Jose G
Jose G

Reputation: 75

Read Data for the current user?

Hello I would like to know how to read data for the current user logged in and how to retrieve the data. The first code up to updateName function is in another viewController. The function updateName is in another ViewController. I put the data under "User" and then have 2 key value pairs called Firstname = SomeData, LastName = SomeData in Firebase RealTimeDatabse, how can I access the only data for the current User logged in. I updated the code to give credit is the called frank, The problem is that it doesn't read data into it and gives a null value, I know why because since the user is creating it user account the uid is null. So how do i save the name of the user when the user is creating it account and call it back for its profile can you please help me Thanks for the Help in advance

        let nameDB = Database.database().reference().child("User")
    let nameDictionary = ["FirstName" : fullName.text!, "LastName" : lastName.text!]   
    let uid = Auth.auth().currentUser?.uid
    Auth.auth().createUser(withEmail: email.text!, password: password1) { (userInformation, someKindOfError) in

            if someKindOfError != nil {
                print(someKindOfError!)
            } else {
                //success

             nameDB.child(uid!).setValue(nameDictionary) {
                    //error 
                    (error, reference) in
                    if error != nil{
                        print(error!)
                    } else  {
                        print("Name saved Sucessful")

                    }
                }
                print("Registration successful!!")

            }
        }

   //Different viewController
   func updateName() {

    let userID = Auth.auth().currentUser?.uid

    let fullName = Database.database().reference().child("User").child(userID!)



    fullName.observeSingleEvent(of: .value) { (snapShot) in

       print("Enter snapShot")
        if let snapShotValue = snapShot.value as? [String : String]{

            print("Entering to grab the data")
            let first = snapShotValue["FirstName"] //as? String
            let last =  snapShotValue["LastName"] //as? String
            print(first, last)

            self.fullName.text = "\(first) \(last)"

        }
    }

Data structure:

 {
"User" : {
"fTKigbhqdVfis9SFk7ofsq1k9Gw1" : {
  "FirstName" : "Alfred",
  "LastName" : "case"
}
 }

 }

Upvotes: 1

Views: 465

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

You're writing the data for a user like this:

let nameDB = Database.database().reference().child("User")
nameDB.childByAutoId().setValue(nameDictionary)

And then you read it with this:

let fullName = Database.database().reference().child("User").child(userID!)
fullName.observeSingleEvent(of: .value) { (snapShot
  ...

If you look carefully the paths are different. You write to /User/$autoId and read from /User/$uid. A user's UID is not the same as an ID that is generated by childByAutoId.

You probably want to write to:

let nameDB = Database.database().reference().child("User")
let uid = Auth.auth().currentUser?.uid
nameDB.child(uid).setValue(nameDictionary)

Upvotes: 1

Related Questions