I'm implementing a method in a service that is used to initialize indexedDB Data. The method should call a webapi in method Ordini_Sincronizza, get the results and process the indexed db.
The code is this:
SyncCustomer(codCli: string): Observable<boolean>
{
this.mdService.Ordini_Sincronizza(codCli).subscribe(
data => {
this._DBService.Configurazione.clear().then(
act => {
let itm = new ConfigurazioneFlat();
itm.codIva = data.configurazione.codIva;
itm.notOrd = data.configurazione.notOrd;
itm.ordini_nota = data.ordiniArticoli.nota;
this._DBService.Configurazione.add(itm);
console.log('INITIALIZED: Configurazione_Flat');
}
);
this._DBService.Configurazione_Sconti.clear().then(
act => {
for (let itm of data.configurazione.sconti) {
this._DBService.Configurazione_Sconti.add(itm);
}
console.log('INITIALIZED: Configurazione_Sconti');
}
);
this._DBService.Configurazione_TipiOmaggio.clear().then(
act => {
for (let itm of data.configurazione.tipiOmaggio) {
this._DBService.Configurazione_TipiOmaggio.add(itm);
}
console.log('INITIALIZED: Configurazione_TipiOmaggio');
}
);
this._DBService.Listino_Testata.clear().then(
act => {
for (let l of data.listini) {
this._DBService.Listino_Testata.add(l.testata);
}
console.log('INITIALIZED: Listino_Testata');
}
);
return Observable.of(true);
},
error => {
console.log(error);
return Observable.of(false);
});
return Observable.of(null);
}
The method is placed inside the SyncService ( typescript service ) and returns true if all works fine or false in case of error.
Now I want to use this service method in a component, I'm doing this:
DownloadData()
{
this.syncService.SyncCustomer(this.p_codCli).subscribe(
data => {
alert('ok');
},
error => {
alert('err');
console.log(error);
});
}
But the strange behavior that I'm facing is that I get immediately the alert('ok') and after I receive the logs message in console.
I think its a sync/async calls problem for sure but I don't know how to manage it.
Thanks to support