Hi,
I’m creating an app which needs to execute several functions before displaying data on screen. The issue is sometimes no data appears on screen because it seems that it’s not waiting the execution of all tasks.
Actually, I need to do 6 steps
- 1.Get User location
- 2.Show Map
- 3.Fetch Data From Firebase
-
- Fetch Second Data From API
-
- Put all data in new array
-
- sort Array Descending by date
Only after that, the app can display data into by Ngif
Sometimes, all working well but not all the time. Data don’t appear on screen even though I can well see data printed into my console.log
How can I force the interface () to wait all execution in my page.ts ?
Below my code for page.ts which I put in ngOnInit() {}
const optionsGeolocalisation = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: Infinity
}
// 1.Get User location
this.geolocation.getCurrentPosition(optionsGeolocalisation)
.then((resp) => {
// 2.Show Map
this.showMap(resp.coords.latitude, resp.coords.longitude);
this.userLatitude = resp.coords.latitude
this.userLongitude = resp.coords.longitude
// 3.Fetch Data From Firebase
this.DBFirebase.collection("alertArray")
.valueChanges()
.subscribe(docs => {
docs.forEach(doc => console.log(doc));
docs.forEach(doc => this.allsArrayFinal.push(doc));
});
}).catch((error) => {
console.log('Data from Firebase error : ', error);
});
// 4. Fetch Second Data From API
this.readApi(url).subscribe((data) => {
this.alertsFromdApi = data;
for (const [number, detail] of Object.entries(this.alertsFromBustedApi.alerts)
)
this.allsArrayFinal.push(detail)
// 5. Put all data in new array
for (let alert of this.allsArrayFinal) {
var newAlert : AlertInApp = {
date : alert.date,
alertTitle : alert.alertTitle
};
this.alertsArray.push(newAlert);
this.addMarkersToMap(newAlert)
}
//6. sort Array Desending by date
this.alertsArray.sort((a,b) => 0 - (a.date > b.date ? 1 : -1) )
this.dataAvailable = true
});
Many thanks!!!