Uncaught TypeError: Cannot read properties of undefined (reading 'map') | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

<div> Tags:{' '} <div> {tags.map((tag, i) => ( <span key={i}>{tag}</span> ))} </div> </div> The above section is causing error.

8th Nov 2022, 3:41 PM
Asha
Asha - avatar
2 Answers
+ 2
Just now found out the solution : https://www.webtips.dev/solutions/fix-cannot-read-properties-of-undefined-reading-map Simply insert ? as shown below: {tags?. Map((tag, i) => ( And the error is resolved!
8th Nov 2022, 3:46 PM
Asha
Asha - avatar
+ 1
In order to fix the error, you need to make sure that the array is not undefined. In order to do this, the simplest way is to use optional chaining. {posts?.map(post => <Card details={post} />)} You can use optional chaining by introducing a question mark after the variable. You can also use a logical AND, or use an if check prior to rendering to prevent running into issues.
8th Nov 2022, 3:52 PM
Asha
Asha - avatar