我试图从我的firebase数据库中收到所有用户的全名,我现在正在做这样的事情:
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let friend = FriendsModel()
friend.fullname = dictionary["fullname"] as! String?
friend.email = dictionary["email"] as! String?
friend.birthDate = dictionary["Birth date"] as! String?
friend.provider = dictionary["provider"] as! String?
print(friend.fullname)
}
})

但postDict [" fullname"]返回nil。
firebase数据库的图片: Firebase Database
func fetchUsers () {
ref.observeSingleEvent(of:.value, with: { snapshots in
if snapshots.exists() {
guard let snapshot = snapshots.children.allObjects as? [FIRDataSnapshot] else { return }
for eachSnap in snapshot {
guard let eachUserDict = snapshots.value as? Dictionary<String,AnyObject> else { return }
// eachUserDict is an object/ dictionary representation for each user
//this prints all the information of the users
print (eachUserDict)
let fullName = eachUserDict["fullname"] as? String
//this returns nil :(
print(fullName)
}
}
})
}
&#13;
这是我收到的照片: enter image description here
答案 0 :(得分:0)
我认为你并没有告诉ref
在哪里观察。改变这一行
ref.observeSingleEvent(of: .value, with: { (snapshot) in
到
guard let id = FIRAuth.auth()?.currentUser?.uid else {return}
ref.child("users").child(id).observeSingleEvent(of: .value) { (snapshots) in
更新: 如果你想获得所有用户的全名,那么试试这个:
ref.child("users").observeSingleEvent(of:.value) { snapshots in
if snapshots.exists() {
guard let snapshot = snapshots.children.allObjects as? [FIRDataSnapshot] else { return }
for eachSnap in snapshot {
guard let eachUserDict = snap.value as? Dictionary<String,AnyObject> else { return }
// eachUserDict is an object/ dictionary representation for each user
let fullName = eachUserDict["fullName"] as? String
}
}