简体   繁体   English

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

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

Greetings my objective is to get a nested value in my database by key.问候我的目标是通过键在我的数据库中获取嵌套值。 In this case I just want to get the Username of the current user that's logged in.在这种情况下,我只想获取当前登录用户的用户名。

The structure of my db is as follows:我的数据库结构如下:

数据库结构

My current solution is:我目前的解决方案是:

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

I want to diverge from this approach since I don't know what more properties the user will get, and any db change will not pass the username, is there any way to search on the database by key only and get it's respective value.我想与这种方法不同,因为我不知道用户将获得更多属性,并且任何数据库更改都不会传递用户名,有没有办法仅通过键在数据库上搜索并获取其各自的值。 I am aware that the orderByChild and orderByKeys exists but I can't understand exactly how they work or if they are useful for this specific case.我知道 orderByChild 和 orderByKeys 存在,但我无法确切理解它们是如何工作的,或者它们是否对这种特定情况有用。

Using object instead of list I can easily access the retrieved dictionary by its key:使用object而不是 list 我可以通过它的键轻松访问检索到的字典:

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

I'd rather use object :我宁愿使用object

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM