我的代码存在问题:
问题在于:
如果param.qtamodificata == false,代码运行正常,但如果params.qtamodificata == true(并调用getQuantities函数),则由于异步调用而返回“undefined”...
我需要以同步方式设置代码流,以便仅在所有过程完成后才返回结果...
如何解决?
感谢支持
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;
}
}
));
}
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;
}));
}
this.orderService.getOrderRow(params, 'test')
.subscribe(list => {
console.log('onlineorder3');
console.log(list);
console.log('onlineorder3');
}