How can I fetch time from Firestore using a promise in Angular CLI 8, but the array is empty because the promise has not resolved yet? How can I ensure the array is only called after the getTime() function has finished executing?
example.service.ts
teste: [];
getTime() {
this.userCollection.ref.get()
.then(res => {
if (res.docs.length == 0) {
alert('No timestamps available at the moment, please wait')
} else {
res.forEach(ponto => {
console.log(ponto.id);
console.log(ponto.data().time.seconds * 1000);
this.time.push(new Date((ponto.data().time.seconds * 1000)));
})
}
}).catch(err => {
console.log(err);
})
}
example.component.ts
ngOnInit() {
this.mainService.getTime();
console.log(this.mainService.time);
}
I want the array variable to be populated by the time data when I access it.