javascriptfirebasefirebase-realtime-database

About FIREBASE => Angular , how to read data in FIREBASE when we use Push method to write data


I have a problem when I want to read firebase DATA, I can't retrieve data.

First time I write data in firebase like that:

    saveJoueurPoule(poule: any,newJoueur: any){
      console.log("poule creation" + poule);
      firebase.database().ref('/poule1).push(newJoueur);
    }

newJoueur is an object:

    export class Joueur {
        //   photo: string;
        constructor(public nom: string , 
               public prenom: string,
               public poule: number, 
               public point: number,
               public victoire: number ,               
               public defaite: number,
               public nbdejeu: number
        ){}

Writing the data works nicely:

Firebase Poule

But when I want to read data firebase "Poule1" like this:

    firebase.database().ref('/poule1').on('value', (data) => {
        this.joueurClassement = data.val() ? data.val() : [] ;
        console.log( this.joueurClassement[0].nom);=> undefined

My object joueurClassement is empty every time (I think).

But if I use a set method to write => firebase.database().ref('/joueurs').set(this.joueur); my reading works very well - why?


Solution

  • Thank you FRank for yout fast answer, it's better but it doesn't work.

    My CODE:

    firebase.database().ref('/poule1').on('value', (data) => {
        data.forEach((child) => this.joueurClass = data.val())
        console.log("Non  = "  + this.joueurClass.nom )
        })
    

    Console display "Non = undefined " so i think JoueurClass is empty. May be , have to initialize a new Object joueurClass ?

    an other question: there are several data and for each child the result overwrite joueurClass , i would like in my object joueurClass all DAta , exemple:

    joueurClass [{vital,arnaud,0,0,0,0,0},{frank,ldjldldld,0,0,0,0,0}]
    

    i hope to be clear. thank you so much for your help