How to make alert with name of child firebase?


#1

Hi every body

I need to show alert with text like “Welcome USER1”.
USER1 is a value that stored in firebase database.

My tree like this

in my .ts file i used this way to get the data

public usersList: FirebaseListObservable<any>;
...
constructor(..., public afd: AngularFireDatabase) {}
showWelcome(){
this.usersList = this.afd.list('/users/');
...
}

So any body know how I can display the fname in alert ?


#2

Well you need to know the user ID which is “-Kp56E…”

So if you implemented some kind of login using firebase you take the userId from it and call

this.usersList = this.afd.list('/user/" + userId);

And you should get the data from it and just access the username like so

this.userLists.fname

#3

Thanks @kgaspar
I will try to check your solution, wish i can understand the way to solve my problem.


#4

Is there any way I can get the fname without insert the id ?

I think there is something like foreach to get the value of all fname ?


#5

Yeah, you get the data in a json object. Then you can easily iterate trough it.

Something in the line of

for(let user in this.userLists){
    console.log(user.fname);
}

#6

Thank u so much

Yes I think this is the way to get the value of fname but I don’t know why it show me an error in user.fname :frowning: :cry:

The text of error " fname does not exist on type ‘string’ "


#7

Ahh yeah my bad, that actually gets the attribute name, so thats why its a string.

for(let key in userLists)
    if (this.userLists.hasOwnProperty(key)) {
        console.log(key + " -> " + this.userLists[key]);
}

#8

thank u for helps

I could find the solution and I wrote it to help other developers in the feature.

this.usersList = this.afd.list('/Users/');
    this.usersList.subscribe(data => {
      data.forEach(user => {
        alert("Welcome " + user.fname);
        }
      });
    });