I use a loop to retrieve the data stored in Firebase thanks to the subscribe function and forEach. When I do a console.log of my datas in the subscribe ("=== SUBSCRIBE ===") I have the good result, but when I do a console.log outside ("=== HOME ===" and "=== RETURN ==="), I have this result :
I suppose it comes from the fact that it is asynchronous, how to fix this problem please ?
This is my code :
statut: FirebaseListObservable<any[]>;
statuts: Array<any> = [];
// Données à enregistrer dans FireBase
dateOfTheDay: string;
dayOfWeek: number;
constructor(public afDB: AngularFireDatabase, public storage: Storage) {
moment.locale('fr');
this.dateOfTheDay = moment().format('L'); // Date au format : 04/07/2017
this.dayOfWeek = moment().day(); // Numéro du jour de la semaine (Ex : 1 pour lundi)
this.statut = this.afDB.list('/statut');
}
statusToShow() {
this.statut.subscribe(statut => {
statut.forEach(s => {
if (this.dateOfTheDay === s.statut_date_tache) {
if (s.statut_id_tache in this.statuts === false) {
this.statuts[s.statut_id_tache] = s;
}
}
});
console.log('=== SUBSCRIBE ===');
console.log(this.statuts);
});
console.log('=== RETURN ===');
console.log(this.statuts);
return this.statuts;
}
Thanks in advance