我是Swift和Firebase的新手,我想知道为什么我会收到snapshot.value作为?即使用户已登录,NSDictionary仍为nil。
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot.value as? NSDictionary)
if let value = snapshot.value as? NSDictionary{
self.title = value["firstname"] as? String
print("o")
}
print(self.title)
}, withCancel: { (error) in
print(error.localizedDescription)
})
}
//I want it to return the name of the user (Ben) as the title of the navigation bar, but instead it returns nil
数据库是: {
"users" : {
"-MBzED86cdVbvBNcYdpY" : {
"email" : "bb@gmail.com",
"firstname" : "benjamin",
"username" : "bb"
},
"-MBzEWEQ3cX6d5aJOByR" : {
"email" : "bslou.row@gmail.com",
"firstname" : "bslou",
"username" : "bbbslou.row"
}
}
}
当我打印快照时,程序返回“Snap(mDNGl4fQXEMx1wPnjGuhILp5f8j1)”
最佳答案
数据库中“用户”下的子项不是Firebase Auth用户ID。它们看起来像随机生成的推送ID(又称“自动ID”)。使用UID的查询根本不会找到这些子级。请注意,快照的ID根本与数据库不匹配。快照快照为空-不包含任何数据。
您将不得不回到创建这些子代并使用用户的实际UID的代码,而不是使用自动ID。
关于ios - 为什么快照。值作为字典返回nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62921263/