I'm experiencing an issue with my code:
The problem arises when param.qtamodificata is set to true, causing the code to return "undefined" due to asynchronous calls. However, everything works fine if params.qtamodificata is false.
I am seeking a solution to ensure the code flow remains synchronous, allowing the result to be returned only after the entire process has been completed.
Any suggestions on how to address this issue?
Thank you for your assistance
getOrderRow
public getOrderRow(params): Observable<any>
{
//OFFLINE SWITCH OK
var res = { headers: [], data: [] }
return Observable.fromPromise(this._WebDBService.sysConfigurazione.toArray().then(
data => {
var art = JSON.parse(data[0].ordinidatajson).filter(
function(itm,index){
return itm.codart.v === params.codart;
}
);
res.headers = JSON.parse(data[0].ordiniheaderjson);
if(params.qtamodificata == true)
{
this.getQuantities(art[0].codart.v,null,params.udmmodificata, params['qtaord'+ params.udmmodificata]).subscribe(
qtys =>{
art[0].qtaord1.v = qtys.dqta1;
art[0].qtaord2.v = qtys.dqta2;
art[0].qtaord3.v = qtys.dqta3;
res.data = art[0];
return res;
}
);
}
else
{
res.data = art[0];
return res;
}
}
));
}
getQuantities
public getQuantities(codart: string, codvar: string, idqta: number, qta: number): Observable<any>{
return Observable.fromPromise(this._WebDBService.tbArticoli.where({codart: codart}).toArray().then(
data =>
{
console.log('getQuantities');
var qtys = {
dqta1: 0,
dqta2: 0,
dqta3: 0
}
var a = data[0];
switch (idqta) {
case 1:
qtys.dqta1 = +qta;
if (a.rapudm12 > 0 && a.codudm2)
qtys.dqta2 = qta / a.rapudm12;
if (a.rapudm23 > 0 && a.codudm3)
qtys.dqta3 = qta / a.rapudm23;
break;
case 2:
qtys.dqta2 = +qta;
if (a.rapudm12 > 0 && a.codudm1)
qtys.dqta1 = qta / a.rapudm12;
if (a.rapudm23 > 0 && a.codudm3)
qtys.dqta3 = qta / a.rapudm23;
break;
case 3:
qtys.dqta3 = +qta;
if (a.rapudm23 > 0 && a.codudm2)
qtys.dqta3 = qta / a.rapudm23;
if (a.rapudm12 > 0 && a.codudm1)
qtys.dqta1 = qta / a.rapudm12;
break;
}
return qtys;
}));
}
Function Call
this.orderService.getOrderRow(params, 'test')
.subscribe(list => {
console.log('onlineorder3');
console.log(list);
console.log('onlineorder3');
}