简体   繁体   中英

Retrieve user data from Firebase database and display in user profile textfields

I want to retrieve user data from the Firebase database (data such as: username, email, age, gender, height) and display it in text fields that are located in my UserProfileViewController. I have successfully been able to store the user data inside my database when the user signs up and creates a profile. However, I am having trouble getting that data back from the database and displaying it inside the users profile view controller. How would I be able to display the user age in the ageTextField, user gender in the genderTextField, etc.?

I have tried to create a dictionary for the values (username, email, age, gender, height, etc.) in the CreateProfileViewController, but it did not seem to work when I tried to retrieve them in the UserProfileViewController. I was wondering if someone could help me with this problem?

This is part of my CreateProfileViewController, which stores the users data into the database:


//reference database
var ref : DatabaseReference!
ref = Database.database().reference().child("users")

 func profile(){

//get data from the current user who signed up (their uid and email), so that the profile data can be stored under the same user) 

        let key = ref.childByAutoId().key 
        let email = Auth.auth().currentUser?.email 


let user = [
                    "id": key,
                    "email": email,
                    "age": ageTextField.text! as String,
                    "gender": genderTextField.text! as String,
            "weight": weightTextField.text! as String,
            "height": heightTextField.text! as String,
            "monthlyGoal": monthlyGoalTextField.text! as String
        ]

self.ref.child(key).setValue(user)

}

 @IBAction func submitProfile(_ sender: Any) {
        profile()
        self.performSegue(withIdentifier: "toSegue", sender: self)
        print("User profile created!")//this takes them to the home page view controller once they successfully sign up and create a profile.

    }

Use uid as key for storing user details. Use let key = Auth.auth().currentUser?.uid instead of let key = ref.childByAutoId().key

func profile() {
    guard let key = Auth.auth().currentUser?.uid else { return }
    let email = Auth.auth().currentUser?.email 
    let user = ["id": key,
                "email": email,
                "age": ageTextField.text! as String,
                "gender": genderTextField.text! as String,
                "weight": weightTextField.text! as String,
                "height": heightTextField.text! as String,
                "monthlyGoal": monthlyGoalTextField.text! as String]
    self.ref.child(key).setValue(user)
}

Retrieve user details from the Firebase using the currentUser?.uid

func getUserDetails() {
    guard let key = Auth.auth().currentUser?.uid else { return }
    self.ref.child(key).observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? [String: Any]
        self.emailTextField.text = value?["email"] as? String
        // ...
    }) { (error) in
        print(error.localizedDescription)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM