Skip to content
Advertisement

React JS fetching data (error: Cannot read properties of undefined)

i am just trying to learn JavaScript. I have to create a web application for school. Now i am trying to fetch data from a self written api. The backend is written with express, the frontend with JavaScript. I have got a overview page, where all products are shown. After clicking on the detail button, the user should be able to view the details of the selected product. For this i use this code.

JavaScript

If i run this code and don’t refresh the page by myself, the correct value (pk_article_nr) is shown. If i refresh the browser manually there is this error

TypeError: Cannot read properties of undefined (reading ‘pk_article_nr’)

This data are shown in the console:

JavaScript

Could you please help me? I haven’t found anything that helps me. Maybe i just searched for the wrong thing.

Thank you, Max

Advertisement

Answer

You should change

JavaScript

to

JavaScript

Reason for this to happen:

React wants to access the property before mounting, while the property has not yet received any content

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement