【发布时间】:2020-05-17 16:40:41
【问题描述】:
我正在尝试使用 subscribe 向 this.voucherDetails 返回值,但它似乎不起作用。
以下是对具有订阅功能的getVoucherDetails函数的请求。
voucherIDs: string[];
voucherDetails: Promise<any>[];
this.voucherIDs = ['JV-2005-50','JV-2005-40','JV-2005-30'];
this.voucherDetails = this.voucherIDs
.map(id => this.getVoucherDetails(id));
Promise.all(this.voucherDetails)
.then(() => this.printService.onDataReady());
以下是订阅用户服务的函数,它从数据库中获取数据。
getVoucherDetails(voucherid) {
var splitvoucher = voucherid.split('-');
var vouchertype = splitvoucher[0];
var vouchermy = splitvoucher[1];
var voucherno = splitvoucher[2];
var vouchernotopass = vouchermy+"-"+voucherno;
var voucherdate;
var enteries;
this.userService.getVoucher(vouchernotopass,vouchertype).subscribe(
res => {
voucherdate = res['voucherdate'];
enteries = res['enteries'];
return { "vouchertype":vouchertype, "vouchernumber": vouchernotopass, "voucherdate": voucherdate, "enteries":enteries};
}
);
}
但它返回未定义。在浏览了几篇帖子后,我了解到您无法从 .subscribe 中返回。但我无法理解如何解决这个问题。
【问题讨论】:
标签: angular typescript