![](/img/trans.png)
[英]Retrieve Firebase database specific value depending on key with angular 5
[英]How to get value in Firebase Database (Angular) by key?
問候我的目標是通過鍵在我的數據庫中獲取嵌套值。 在這種情況下,我只想獲取當前登錄用戶的用戶名。
我的數據庫結構如下:
我目前的解決方案是:
this.db.list("/Users/"+this.userId).valueChanges().subscribe(details => {
this.username = details[3]
}
我想與這種方法不同,因為我不知道用戶將獲得更多屬性,並且任何數據庫更改都不會傳遞用戶名,有沒有辦法僅通過鍵在數據庫上搜索並獲取其各自的值。 我知道 orderByChild 和 orderByKeys 存在,但我無法確切理解它們是如何工作的,或者它們是否對這種特定情況有用。
使用object而不是 list 我可以通過它的鍵輕松訪問檢索到的字典:
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;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.