[英]Promise returns undefined value
I need to retrieve the result of my Api call outside the promise but the value I get is undefined.我需要在 promise 之外检索我的 Api 调用的结果,但我得到的值是未定义的。
In OrderService:在订单服务中:
public async getOrderPrice(device: string) : Promise<any> {
this.urlOrderPrice = this.urlOrderPrice.replace("device", device);
return await this.http.get(this.urlOrderPrice).toPromise();
}
In OrderDetailsComponent:在 OrderDetailsComponent 中:
public orderPrice: any;
ngOnInit(): void {
this.getOrder()?.then(data => { this.orderPrice = data; console.log(this.orderPrice) });
}
private async getOrder() {
const id = this.route.snapshot.paramMap.get('id');
let order;
if (id != null) {
order = this.orderService.getOrderPrice(id).then((result) => {
return result.results[Object.keys(result.results)[0]].val;
});
}
return order;
}
in getOrder()
when your id==null
, then returned order is undefined
在
getOrder()
中,当您的id==null
时,返回的订单undefined
also remove extra await from rest call还从 rest 呼叫中删除额外的等待
public async getOrderPrice(device: string) : Promise<any> {
this.urlOrderPrice = this.urlOrderPrice.replace("device", device);
return this.http.get(this.urlOrderPrice).toPromise();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.