Skip to content
Advertisement

I cannot find the error TypeError: Cannot read properties of undefined (reading ‘name’)

I’m new to react and I need some help when I´m trying to bring some “products” from an array of objects that I have in one file of my react app called “products.js”.

The thing is that I am able to draw the products within the array in the Home Screen. However when I try to draw those products on my details products page (this one is a page where I can see an individual product details) “ProductDetails.js” I’m getting the error (regardless if I try to bring product.name or product.price or whatever):

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

In order for you to have an idea about what I’m talking about I will share to you some snippets from my code to put you in context. I will Start with the exact error message and I will finish with a Glance of My Project Dependencies.

Error Message

JavaScript

this is the View I’m having TROUBLE with and is not only with the “product.name” I happens with anything “product.price” “product.image”… this is the details product screen, the view that is supposed to show me each individual product with its details on its own page, which I called “ProductDetails.js”

JavaScript

This is the File with the array containing the products I want to draw on the above product details screen. this file is the one called “products.js”:

JavaScript

This is the view for the Home Screen, where I show all of the products within the above array, this one works just fine and draws every product within the array on the screen. this one is called “HomeScreen.js”

JavaScript

This is the Product component, this one basically draws each individual product within the array on the above Home Screen view. This one as the above Home Screen also works just fine, it brings me the product with its name, its price and image. I did call this component “Product.js”*

JavaScript

“App.js” file, so you have a better understanding about what is going on on my project

JavaScript

This is My “package.json” file with dependencies in my project, just in case It helps to help me with this

JavaScript

Here you have an Screenshot of the error message on the products details view
error

Here you have an Screenshot of the page that works the Home Screen
enter image description here

Advertisement

Answer

Your issue is that your .find() method is returning undefined, so you can’t access properties on product such as .name as it is undefined. The .find() method will return undefined when the callback function doesn’t return a truthy value for any of your items within your array.

In this case, your callback will only return true when your product id matches the id exactly (in both value and type) of the id used in your URL:

JavaScript

However, the above will never be true, as p._id in your data is a number type, and match.params.id is a string type, so the two are never considered equal under strict equality ===. As a result, .find() never finds a match and you get undefind. Instead, you can use “loose” equality comparison ==, which doesn’t need the types to match:

JavaScript

Alternatively, you can convert the type manually using a method such as Number() and stick with ===:

JavaScript

Other things to try

I’ve noticed that this question has gathered quite a lot of attention, so I thought I’d add another common reason why you might see this error (note these reasons don’t apply to OPs code).

Firstly, the error means that whatever you’re using .name on has a value of undefined. Typically this can happen if you’re making an API call with something like fetch() or doing some other asynchronous task within a useEffect() which sets the value of whatever you’re using .name on. There are two ways to combat this issue, one way is to use optional chaining to prevent access of the .name property if the object is null or undefined, eg:

JavaScript

Or, another way is to assign your state value so that it initially isn’t undefined, eg:

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