In componentOne.ts i am sending the data through sharedService as,
this.sharedService.sendData(this.getCheckedProduct);
In componentTwo.ts i am subscribing the data like,
productList: any = [];
getAllProducts: any = [];
ngOnInit() {
this.sharedService.getData().subscribe(data => {
this.productList = data;
this.getProduct();
});
}
I am getting the data here in productlist then i need to call the function this.getProduct() which has the following,
getProduct() {
let tempArray = [];
this.productList.forEach(element => {
this.appService.getRest(AppConfig.settings.product.getProducts + '/' + element.product_obj_id).subscribe(res => {
tempArray.push(res.data);
});
});
this.getAllProducts = tempArray;
}
I need to pass the id element.product_obj_id to get the necessary data of that id..
I have tried changing the above like this,
ngOnInit() {
this.sharedService.getData().subscribe(data => {
data.forEach(element => {
this.getProduct(element);
})
});
}
async getProduct(element) {
let asyncResult = await this.appService.getRest(AppConfig.settings.product.getProducts + '/' + element.product_obj_id).toPromise();
this.getAllProducts.push(asyncResult['data']);
}
Inside async getProduct(element) function i am, getting the data of this.getAllProducts but in html i am not getting the data.
HTML:
<div *ngFor="let product of getAllProducts">
Getting data
</div>
If i changed the above with async
<div *ngFor="let product of getAllProducts | async">
Getting data
</div>
I am getting error as,
ERROR Error: InvalidPipeArgument: '' for pipe 'AsyncPipe'
at invalidPipeArgumentError
To explain in detail, i need to send a data through sharedservice in componentone and recieve it in componenttwo then need to get the product_obj_id from that shared service data.
The on passing each id, i will get the data of that particular product and i need to store the final data recieved from this.appService.getRest(AppConfig.settings.product.getProducts + '/' + element.product_obj_id).toPromise(); to getAllProducts..
AppConfig.settings.product.getProducts
is the url ..
How to achieve it in a async way.. UPto this i am getting data
this.getAllProducts.push(asyncResult['data']);
but outside the function i am not getting the value, Also the getAllProducts no more working on any scenario in html..
In scenario 1 explained at top of this question i have given the following
this.getAllProducts = tempArray;
This one gives empty array as value and so only i am trying with async function.
In simple words i need to get the final data from this.getAllProducts which will be recieved from the service with get method for which i need to pass an id in the url..