I am encountering an issue where I cannot retrieve object values in the form for editing/updating. The specific error message is as follows:
ERROR TypeError: Cannot read properties of undefined (reading 'productName')
at UpdateProductComponent.ngOnInit (update-product.component.ts:63:33)
at callHook (core.mjs:2576:22)
at callHooks (core.mjs:2545:17)
at executeInitAndCheckHooks (core.mjs:2496:9)
at refreshView (core.mjs:11622:21)
at refreshEmbeddedViews (core.mjs:12812:17)
at refreshView (core.mjs:11631:9)
at refreshComponent (core.mjs:12858:13)
at refreshChildComponents (core.mjs:11348:9)
at refreshView (core.mjs:11657:13)
Within my productservice.ts file, I have the following method defined:
getProductById(id: number){
return this.http.get<Product>(`${this.baseUrl}/getProductById/${id}`);
}
This function successfully retrieves the id or productId without any issues,
In my main component, I can redirect to a new page/UpdateProductComponent using the following code:
updateproduct = (productId: any) => {
this.router.navigateByUrl('/updateproduct', productId)
};
Within my UpdateProductComponent, the implementation looks like this:
product: IProduct;
id: any;
ngOnInit() {
this.id = this.route.snapshot.paramMap.get('id');
this.productService.getProductById(this.id).subscribe((data: Product)=>{
this.product = data;
console.log(this.product), // successfully retrieves the data
this.updateForm.patchValue({
// productId: this.id, // works
// productName: 'asdas', // works
productId: this.product.productId, // does not work
productName: this.product.productName // does not work
})
});
}
I can confirm that the data is fetched correctly under network tab > payload for that product in the format: [{"productId":"2","productName":"23123","productDescription":"wqeq","productCategory":"qewqweq","units":23}]
*** Update *** After updating my service to return an Observable, the error is resolved but the values are still not populating in the form.