I can't find a valid solution for following case
I want to create the following loop :
- ForEach elemenent (A) of an array
- Populate a new array (B)
- If key doesn't exists in array (B) call a firebase service getting data
- Verify data and update Statistic
- at the end of all (A) key, generate statistic of execution and expose...
I need to wait and chain the response of Firebase data before continue the loop, and the problem is that I need to interact with data from firebase in the main loop ( so I can't implement it in subscribe or then )
This is the Part of code that i want to create :
// Here i check if user exist in array (B)
let IDUtente = RifEvento.esisteUtente(Ordini.IDUtente) ;
// If not exists i'll get data from FIREBASE
if (IDUtente == -1) {
let NewUser = new TipoStatUtente() ;
NewUser.Chiave = Ordini.IDUtente ;
IDUtente = RifEvento.Utenti.push(NewUser) ;
IDUtente = IDUtente - 1 ;
// I NEED to wait the end of this function before continue with loop
this.statoUtente.getUser(IDUtente).then(dati => {
console.log('Sottoscritto dati utente') ;
let user : TipoSingoloUtente = dati ;
NewUser.sonoscuola = user.sonoscuola ;
if (!NewUser.sonoscuola) NewUser.Intestazione = user.Cognome + ' ' + user.Nome ; else NewUser.Intestazione = user.Scuola.Nome ;
if (NewUser.sonoscuola) RifEvento.NumScuole += 1 ; else RifEvento.NumUtenti += 1 ;
})
}
console.log(IDUtente) ;
// Utente Esiste aggiorno le sue statistiche
RifEvento.Utenti[IDUtente].Commissioni += Ordini.costoCommissione ;
RifEvento.Utenti[IDUtente].Incasso += Ordini.parziale ;
if (Ordini.Tipo == 'T') {
RifEvento.Utenti[IDUtente].NumTicket += Ordini.numbiglietti ;
} else {
RifEvento.Utenti[IDUtente].NumIscritti += 1 ;
RifEvento.Utenti[IDUtente].NumBallerini += Ordini.IDBallerini.length ;
}
}
And this is the function :
getUser(IDUtente) : Promise<TipoSingoloUtente> {
return this.db.object('user/' + IDUtente).map(users => {
return users ;})
.first()
.toPromise(); }