Reputation: 85
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.
Upvotes: 3
Views: 2980
Reputation: 1297
I'd rather use object
:
this.db.object("/Users/" + this.userId).valueChanges()
.subscribe(details => {
this.username = details.Username;
});
Upvotes: 0
Reputation: 85
Using object instead of list I can easily access the retrieved dictionary by its key:
this.db.object("/Users/"+this.userId).valueChanges().subscribe(details => {
this.username = details["Username"]
}
Upvotes: 2