Firebase具有侦听客户端身份验证状态更改的$onAuth方法。如果客户端经过身份验证,回调将被传递给一个包含uid的对象。下面是我的问题:基于这个uid,如何获得这个对象的$id。
这就是我的数据在防火墙中的样子:
profile {
-K2a7k9GXodDriEZrM3f {
email: "ale@aol.com"
gravatar: "https://www.gravatar.com/avatar/1d52da55055f0c8..."
uid: "3f9e0d53-4e61-472c-8222-7fc9f663969e"
name: "ale"
}
}我在用
auth.$onAuth(function (authData) {
if (authData) {
authData.profile = //here I want the $id of the object
}
})我看到的authData响应所能做的就是通过以下方法获取对象的uid:
$firebaseObject(ref.child('profile').child(authData.uid));此authData不包含对象的$id (即-K2a7k9GXodDriEZrM3f)。这就是我正在寻找的,以使用户的信息可用。有办法做到这一点吗?
谢谢。
发布于 2015-11-08 15:28:44
如果对象具有自然唯一的ID,通常应该将对象存储在该ID下。在这些情况下使用push ID不会增加值,只会使事情复杂化。
大多数使用Firebase的开发人员都像文档中的“存储用户数据”部分所演示的那样:他们通过uid存储用户。如果您遵循该指导原则,您可以找到作为authData.uid的密钥。
如果您决定坚持当前的课程,您可以通过以下方式为用户找到关键:
ref.child('profile').orderByChild('uid').equalTo(authData.uid).once('child_added', function(snapshot) {
console.log(snapshot.key());
});但是,您将使数据库更难找到您的用户,从而限制了应用程序的可伸缩性。
https://stackoverflow.com/questions/33593076
复制相似问题