【问题标题】:Firebase: How to get the $id of an object after I authenticate using angularFireFirebase:使用 angularFire 进行身份验证后如何获取对象的 $id
【发布时间】:2015-11-08 10:45:08
【问题描述】:

Firebase 具有 $onAuth 方法,用于侦听客户端身份验证状态的更改。如果客户端通过身份验证,回调将传递一个包含 uid 的对象。 这是我的问题: 根据这个 uid,我怎样才能得到这个对象的 $id。

这就是我的数据在 firebase 中的样子:

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)。这就是我正在寻找的,以便让用户的信息可用。有没有办法做到这一点?

谢谢。

【问题讨论】:

  • $id 是 $firebaseArray 记录的属性,在 $firebaseObject 实例中不存在。如果您想要 $firebaseObject 实例的密钥(不是 OP 在这里寻找的),可以在 obj.$ref().key() 中找到。

标签: angularjs firebase angularfire firebase-authentication


【解决方案1】:

如果一个对象有一个自然的唯一 ID,您通常应该将该对象存储在该 ID 下。在这些情况下使用推送 ID 不会增加任何价值,只会使事情复杂化。

大多数使用 Firebase 的开发人员都按照the "Storing User Data" section in the documentation 的说明进行操作:他们通过用户的 uid 存储用户。如果您遵循该指南,您可以找到密钥为authData.uid

如果您决定坚持当前的课程,您可以通过以下方式为用户找到密钥:

ref.child('profile').orderByChild('uid').equalTo(authData.uid).once('child_added', function(snapshot) {
    console.log(snapshot.key());
});

但是你会让数据库更难找到你的用户,从而限制你的应用程序的可扩展性。

【讨论】:

  • 这正是我想要的。我已使用您提供的链接来存储我的用户数据。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2019-03-21
相关资源
最近更新 更多