Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in my productservice.ts
I got :

TypeScript
getProductById(id: number){
    return this.http.get<Product>(`${this.baseUrl}/getProductById/${id}`);
  }
```


in my UpdateProductComponent:
TypeScript
ngOnInit() {

    this.initializeForm();
    const id = Number(this.route.snapshot.paramMap.get('id'));
    if (id) {
      console.log(id);
      this.productService.getProductById(id).subscribe(
      product => this.product = product)
    }
    else { console.log('id is not defined') }

    this.updateForm.setValue(
    {
      productId: this.product.productId,
      productName: this.product.productName,

    });
  }

  initializeForm()
    {
      this.updateForm = new FormGroup({
        productId: new FormControl({value: '', disabled: true }),
        productName: new FormControl(),
      });
    }

  getProduct(id: number)  {
    this.productService.getProductById(id).subscribe({
       next: product => this.product = product,
       error: err => this.errorMessage = err
    });
  }
}



i was able to get the id/productId fine, but how do I get the rest of the variables?

I am getting this error in developer tools

ERROR TypeError: Cannot read properties of undefined (reading 'productId')
at UpdateProductComponent.ngOnInit (update-product.component.ts:41:31)
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)


What I have tried:

tried several ways to get the values with no luck
Posted
Updated 15-Jun-22 22:33pm
Comments
Richard MacCutchan 16-Jun-22 4:26am    
"at UpdateProductComponent.ngOnInit (update-product.component.ts:41:31)"
Which line is line 41?

If you don't understand an error message, google it: typescript cannot read properties of undefined - Google Search[^]
The top hit explains the problem: Resolving the TypeError: Cannot Read Property of Undefined in JavaScript[^]
 
Share this answer
 
Comments
termine1 16-Jun-22 10:32am    
thank you originalgriff
the error is confusing as I am getting the value of id fine and based on that id/productId I am getting all the json values fine. I can verify in network tab > under payload for that product as follows:
[{"productId":"2","productName":"23123","productDescription":"wqeq","productCategory":"qewqweq","units":23}]

All I need to know is how to get those values populated in the form?
TypeScript
    if (id) {
      console.log(id);
      this.productService.getProductById(id).subscribe(
      product => this.product = product)
    }
    else { console.log('id is not defined') }

    this.updateForm.setValue(
    {
41***      productId: this.product.productId,
      productName: this.product.productName,

    });

If id is not defined, then you will not have set product, so the code starting at 41*** will obviously cause the exception. You need to change your code so the method either returns some value to indicate the problem, or throws a user defined Exception. Maybe something like:
TypeScript
    if (id) {
      console.log(id);
      this.productService.getProductById(id).subscribe(
      product => this.product = product)
    }
    else { Throw ValueError('id is not defined') } // cannot continue in this code block

    this.updateForm.setValue(
    {
41***      productId: this.product.productId,
      productName: this.product.productName,

    });
 
Share this answer
 
Comments
termine1 16-Jun-22 10:32am    
thank you Richard.
the error is confusing as I am getting the value of id fine and based on that id/productId I am getting all the json values fine. I can verify in network tab > under payload for that product as follows:
[{"productId":"2","productName":"23123","productDescription":"wqeq","productCategory":"qewqweq","units":23}]

All I need to know is how to get those values populated in the form?
Richard MacCutchan 16-Jun-22 11:26am    
I don't know the answer but the error is clearly telling you that the variable product has not been initialised. You need to do some debugging to find out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900