【问题标题】:Retrieve user data from Firebase database and display in user profile textfields从 Firebase 数据库中检索用户数据并显示在用户配置文件文本字段中
【发布时间】:2019-06-27 05:01:42
【问题描述】:

我想从 Firebase 数据库中检索用户数据(数据,例如:用户名、电子邮件、年龄、性别、身高)并将其显示在我的 UserProfileViewController 中的文本字段中。当用户注册并创建配置文件时,我已经成功地将用户数据存储在我的数据库中。但是,我无法从数据库中取回该数据并将其显示在用户配置文件视图控制器中。如何在ageTextField 中显示用户年龄、在genderTextField 中显示用户性别等?

我尝试为 CreateProfileViewController 中的值(用户名、电子邮件、年龄、性别、身高等)创建一个字典,但是当我尝试在 UserProfileViewController 中检索它们时它似乎不起作用。我想知道是否有人可以帮助我解决这个问题?

这是我的 CreateProfileViewController 的一部分,它将用户数据存储到数据库中:


//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.

    }

【问题讨论】:

  • 您也可以发布您的代码吗?
  • 嗨,欢迎来到 stackoverflow!请分享一些关于您如何尝试从 Firebase 数据库中检索用户数据的代码。否则很难判断出了什么问题。
  • 显示当用户注册并创建个人资料时如何将用户数据存储在数据库中
  • @RajeshKumarR,这是我用来在 Firebase 数据库中存储用户数据的方法,希望对您有所帮助。
  • @RajeshKumarR,哦,好的,谢谢,我刚刚修复了我的代码的那一部分。你知道我怎样才能成功地从数据库中检索用户数据吗?

标签: ios swift database firebase


【解决方案1】:

使用uid 作为存储用户详细信息的密钥。使用let key = Auth.auth().currentUser?.uid 而不是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)
}

使用 currentUser?.uid 从 Firebase 检索用户详细信息

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)
    }
}

【讨论】:

    猜你喜欢
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多