Skip to content Skip to sidebar Skip to footer

Then Array Empty Promisse

Create a method to fetch time in the firestore by calling the promise, but the array comes empty because the promise has not yet been resolved. How to resolve to call the array onl

Solution 1:

You can use async and await.

In service.

getTime() {
    returnthis.userCollection.ref.get()
      .then(res => {
        if (res.docs.length == 0) {
          alert('Não existem marcacoes até o momento por favor aguarde')
        } else {
          res.forEach(ponto => {
            console.log(ponto.id);
            console.log(ponto.data().time.seconds * 1000);
            this.time.push(newDate((ponto.data().time.seconds * 1000)));
          })
        }
        returntrue;
      }).catch(err => {
        console.log(err);
      })
  }

In component,

asyncngOnInit() {
    awaitthis.mainService.getTime();
    console.log(this.mainService.time);
}

Post a Comment for "Then Array Empty Promisse"