ios - 从 Firebase 数据库中检索用户数据并显示在用户配置文件文本字段中

标签 ios swift database firebase

我想从 Firebase 数据库中检索用户数据(例如用户名、电子邮件、年龄、性别、高度等数据)并将其显示在位于我的 UserProfileViewController 中的文本字段中。当用户注册并创建配置文件时,我已经成功地将用户数据存储在我的数据库中。但是,我无法从数据库中取回该数据并将其显示在用户配置文件 View Controller 中。我如何才能在 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.

    }

最佳答案

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

关于ios - 从 Firebase 数据库中检索用户数据并显示在用户配置文件文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784184/

相关文章:

objective-c - iOS 正则表达式 : Targeting currency names

ios - 在 Objective-C 中序列化异步任务

ruby-on-rails - rails : Validate Unique Pairs

sql - 代码重构 SQL Server 数据库对象名称更改

mysql - 是否可以创建一个 hh :mm time column? 的表

ios - 解析指针值返回 nil 的查询

ios - 删除后正在观察通知

ios - 如何从纯 C 函数获取返回值到 swift?

ios - CLLocationManager 和 CLLocationManagerDelegate 的问题

swift - 如何根据要求在 SceneKit 中添加动画