I need to retrieve the result of my Api call outside the promise but the value I get is undefined.
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:
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
also remove extra await from rest call
public async getOrderPrice(device: string) : Promise<any> {
this.urlOrderPrice = this.urlOrderPrice.replace("device", device);
return this.http.get(this.urlOrderPrice).toPromise();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.