3

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:

db structure

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.

1
  • can you edit with full code? what is db variable? Commented May 24, 2022 at 12:22

2 Answers 2

2

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"]
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'd rather use object:

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

2 Comments

Thank you for the reply. Using object seems to be the appropriate method, however using your code raises an Unkown Property error, the correct way to access it is by using details["Username"].
Sorry, I underestimated the capitalization. Glad to see it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.