我正在设置一个函数,该函数允许用户检索他们存储在数据库中的数据。当用户键入数据值时,当数据库中已存储信息时,它将检查数据并将其显示给用户。如何通过搜索“ icNumber”来检索数据并显示该子值下的所有数据?
这是我要达到的目标的screenshot。
最后,这是我的代码
func displayRecord() {
//guard let uid = Auth.auth().currentUser?.uid else { return }
Database.database().reference().child("Patient_Data").observeSingleEvent(of: .value) { (snapshot) in
guard let name = snapshot.value as? String else { return }
guard let icNumber = snapshot.value as? String else { return }
guard let gender = snapshot.value as? String else { return }
guard let phoneNumber = snapshot.value as? String else { return }
guard let medicationAllergic = snapshot.value as? String else { return }
guard let smokerRecord = snapshot.value as? String else { return }
self.welcomeLabel.text = "Name, \(name)"
self.welcomeLabel.text = "IC Number, \(icNumber)"
self.welcomeLabel.text = "Gender, \(gender)"
self.welcomeLabel.text = "Phone Number, \(phoneNumber)"
self.welcomeLabel.text = "Medication Allergic, \(medicationAllergic)"
self.welcomeLabel.text = "Smoker Record, \(smokerRecord)"
UIView.animate(withDuration: 0.5, animations: {
self.welcomeLabel.alpha = 1
})
}
}
答案 0 :(得分:0)
您可能希望更改您的displayRecord
方法以获取条目ID(“ -Lq_y67Rndy3BLyaKA7Q”),因此一次只能看到一个条目,而不是系统中的每个患者数据。然后,您需要将快照值转换为与数据库中存储的类型匹配的字典。那是guard let dict = snapshot.value as? [String:String] else { return }
行。
func displayRecord(entryId: String) {
Database.database().reference().child("Patient_Data/\(entryId)").observeSingleEvent(of: .value) { (snapshot) in
guard let dict = snapshot.value as? [String:String] else { return }
let name = dict["name"]
let icNumber = dict["icNumber"]
let gender = dict["gender"]
let phoneNumber = dict["phoneNumber"]
let medicationAllergic = dict["medicationAllergic"]
self.welcomeLabel.text = "Name, \(name)"
self.welcomeLabel.text = "IC Number, \(icNumber)"
self.welcomeLabel.text = "Gender, \(gender)"
self.welcomeLabel.text = "Phone Number, \(phoneNumber)"
self.welcomeLabel.text = "Medication Allergic, \(medicationAllergic)"
self.welcomeLabel.text = "Smoker Record, \(smokerRecord)"
UIView.animate(withDuration: 0.5, animations: {
self.welcomeLabel.alpha = 1
})
}
}
您似乎认为快照的值将为String
类型。这一定是为什么要将值强制转换为字符串并转换为多个属性的原因(即使快照值是字符串也无济于事)。
您可能还需要考虑是否所有这些值都应作为字符串存储在后端。