您好,我的目标是通过键在数据库中获取嵌套值。在本例中,我只想获取当前登录用户的用户名。我的数据库的结构如下:我当前的解决方案是:this.db.list("/Users/"+this.userId).valueChanges().subscribe(details => {
this.username = details[3]
}我想偏离这种方法,因为我不知道用户将获得哪些更多属性,并且任何数据库更改都不会传递用户名,是否有任何方法仅按键搜索数据库并获取其各自的值。我知道 orderByChild 和 orderByKeys 存在,但我无法确切理解它们是如何工作的,或者它们对于这个特定情况是否有用。
2 回答
森栏
TA贡献1810条经验 获得超5个赞
使用对象而不是列表,我可以通过其键轻松访问检索到的字典:
this.db.object("/Users/"+this.userId).valueChanges().subscribe(details => { this.username = details["Username"] }
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
我宁愿使用object:
this.db.object("/Users/" + this.userId).valueChanges()
.subscribe(details => {
this.username = details.Username;
});
https://github.com/angular/angularfire/blob/master/docs/rtdb/objects.md#retrieve-data
添加回答
举报
0/150
提交
取消