I am currently using a function called syncOnRoute() that returns a Promise. This function is responsible for synchronizing my data to the API multiple times based on the length of the data, and it returns a string message each time. However, I am facing an issue where the for loop completes before receiving the message from the API server.
Can someone please assist me with this?
syncOnRoute() {
return new Promise((resolve, reject) => {
this.auth.database.executeSql('select * from onroute where HaveData=?',
['yes']).then((res) => { //will retrieve 3 pieces of data from the Database
for (let i = 0; i < res.rows.length; i++) { // will execute 3 times
let params = new HttpParams()
.set('CLAIMGUID', res.rows.item(i)['CLAIMGUID'])
.set('JOBGUID', res.rows.item(i)['JOBGUID'])
.set('Latitude', res.rows.item(i)['Latitude'])
.set('Longitude', res.rows.item(i)['Longitude'])
.set('OnRouteDT', res.rows.item(i)['OnRouteDT'])
.set('Desc', this.auth.getdateformatV2())
.set('title', 'onroute');
this.auth.httpSend('Service_SaveOnRoute', params, 'post').subscribe((msg) => {
console.log('##SUCCESS ONROUTE SYNC' + JSON.stringify(msg)); // receiving return msg from API
},
(err) => {
console.log('##SUCCESS ONROUTE SYNC' + JSON.stringify(err));
}
);
} // the for loop completes before receiving return msg from API
x
Here is my httpSent() method, which returns an Observable
public httpSend(endPoint:string, params:HttpParams , method: string){
switch(method){
case 'post':{
return this.httpClient.post<any>("https://ws-v3test.ventureprise.cloud/Service1.asmx/"+endPoint,params);
}
break;
case 'get':{
return this.httpClient.get<any>("https://ws-v3test.ventureprise.cloud/Service1.asmx/"+endPoint);
}
}
}