Search code examples
javascriptfirebasegoogle-cloud-firestorefork-joinreference-type

Firestore: reference type.How wait for all get() call of evey result


I am getting an Observable Ticket[]> from a firestore DB, one field in ticket is reference type.

When I subscribe for results I use:

 getTickets()
.subscribe(listOfTickets=> {  

     //loop the array
     listOfTickets.forEach(ticket => {

         ticket.personRef.get()  //this is the reference type field
         .then(res => { //getting information
            let person = res.data();
       }
     }         
     .... 
 }    

How can I wait for all the results in:

ticket.personRef.get()

I am trying using forkJoin, but I still do not understand how apply to this. The observable listOfTickets has a lot of results.


Solution

  • Do not use forEach. Use for

      async someFunction() {
        getTickets().subscribe(async listOfTickets => {
          const allTickets = [];
            for (let i = 0; i < listOfTickets.length; i++) {
              await listOfTickets [i].personRef.get().then(snapshot => {
                return snapshot.data();
              }).then(ticket => {
                allTickets.push(ticket);
              })
            }
          console.log(allTickets);
        })
      }