func fetchUser(_ uid: String) {
FIRDatabase.database().reference().child("users").child("g373gSvDcFhXNAGh5V6vg7sU5C02").observe(.childAdded, with: {
(snapshot) in
//TODO...
print(snapshot) //Snap (email) test1@gmail.com
let user = snapshot.value as ? [String: Any]
print(user) //nil
if let _nameLabel = self.nameLabel,
let name = user ? ["name"] as ? String {
_nameLabel.text = "{-} \(name)"
}
}, withCancel: nil)
}
答案 0 :(得分:2)
<强>问题:强>
您使用事件类型.childAdded
进行观察
将一个新的子节点添加到某个位置时触发了childAdded
<强>解决方案:强>
您应该将事件类型从.childAdded
更改为.value
,如下所示:
FIRDatabase.database().reference().child("users").child("g373gSvDcFhXNAGh5V6vg7sU5C02").observe(.value, with: { (snapshot) in
// other code
})