I need to locate a record by its ID using Angular TypeScript, and the array provided for this purpose is shown below
{
"products": [
{
"id": "1731002618",
"imageUrl": "product_1.jpg",
"brand": "Max Home Collection",
"title": "5.2 cu. ft. High-Efficiency Stackable Front-Load Washer in White",
"model": "7L12X165/8045CL",
"rating": 3,
"price1": 34.00,
"cuom1": "case",
"price2": 1.97,
"cuom2": "sq. ft.",
"saving": 22,
"was": 41.48,
"addToCart": "addToCart",
"badges": [
"badge-ECO",
"badge-NLP",
"badge-SB",
"badge-ES"
]
},
...
}
]
}
The code I am working on looks like this
getProductByID(postid:string):Product {
this.http.get<Product[]>(this.configUrl).subscribe(data => {
this.product=data;
});
// return this.http.get<Product[]>(this.configUrl);
return { ...this.product.find(p=>p.id===postid) };
}
ngOnInit() {
this.route.paramMap.subscribe((paramMap)=>{
if(paramMap.has("postid")) {
alert('differentiate '+paramMap.get("postid"));
alert('before call');
var p = this.Service.getProductByID(paramMap.get("postid"))
.subscribe(data=>{ console.log(data);});
})
}
However, I keep getting an error with the find function. Even when trying the filter function, the issue persists. Your assistance on this matter would be greatly appreciated.