如何按键获取 Firebase 数据库 (Angular) 中的值?

How to get value in Firebase Database (Angular) by key?

您好,我的 objective 是通过键在我的数据库中获取嵌套值。 在这种情况下,我只想获取当前登录用户的用户名。

我的数据库结构如下:

我目前的解决方案是:

this.db.list("/Users/"+this.userId).valueChanges().subscribe(details => {
  this.username = details[3]
}

我想偏离这种方法,因为我不知道用户会得到什么更多的属性,而且任何数据库更改都不会传递用户名,有没有办法只按键搜索数据库并得到这是各自的价值。我知道 orderByChild 和 orderByKeys 存在,但我不明白它们是如何工作的,或者它们是否对这种特定情况有用。

我宁愿使用 object:

this.db.object("/Users/" + this.userId).valueChanges()
  .subscribe(details => {
    this.username = details.Username;
  });

使用 object 而不是列表,我可以通过它的键轻松访问检索到的字典:

this.db.object("/Users/"+this.userId).valueChanges().subscribe(details => {
  this.username = details["Username"]
}