我想在我的应用中显示用户列表。我使用firebase中的默认Auth
系统。但反应总是空洞的。
FIRDatabase.database().reference().child("users").queryOrdered(byChild: "email").observe(.value, with: { snapshot in
print(snapshot)
})
但snapshot
总是Snap (users) <null>
答案 0 :(得分:3)
Firebase身份验证系统不会自动将任何内容插入Firebase数据库。我猜你的数据库是空的,这就是snapshot
null
的原因。
您的代码看起来是正确的,但正如我所说,您的数据库中可能没有任何数据可供接收。
根据您想要实现的目标,您应该考虑在您的数据库中存储用户元数据。这样做的一个好处是在用户创建之后。
答案 1 :(得分:2)
默认情况下,向Firebase身份验证用户注册不会修改您的Firebase数据库。身份验证和数据库是两个非常不相关的服务。注册用户后,通常的做法是使用uid
在数据库中保存条目,这样您就可以将这两种服务联系起来:
let auth: FIRAuth? = FIRAuth.auth() // The authentication object
auth?.createUser(withEmail: email, password: password) { (user, error) in
// If registration was successful, `user` is a FIRUser with a uid
if let userId = user?.uid {
let exampleDBPath = FIRDatabase.database().child("users").child(userId)
// Write the user object, for instance a user name or other data, to this path
exampleDBPath.setValue(someJSONAboutTheUser) { (error, result) in
// Now you have a spot to modify your user in the database
}
}
}
从注册创建的FIRUser
与用户尝试登录时获得的对象类型相同,因此您可以通过相同的uid在数据库中找到正确的用户。