What the error means
TypeError: Cannot read properties of undefined (reading 'map')
In plain words: you wrote something.map(...), but something is undefined, so there is nothing to call .map on.
.map() is a method that belongs to arrays. JavaScript is saying: "You asked me to call .map on this thing, but this thing doesn't exist (it's undefined), so I can't."
The problem is almost never .map itself. The problem is whatever comes before the dot.
A simple example that breaks
const user = { name: "Ana" };
const upper = user.hobbies.map(h => h.toUpperCase());
// TypeError: Cannot read properties of undefined (reading 'map')
user has no hobbies property, so user.hobbies is undefined. Calling .map on it crashes.
Common reasons this happens
- A typo or wrong property name, such as
data.item instead of data.items.
- Data hasn't arrived yet. This is very common when fetching from an API. Your code runs before the data is loaded.
- The data has a different shape than you expected. For example, the API returns
{ results: [...] } but you used response.data.
- A function forgot to
return, so it gives back undefined.
Typical fixes
Fix 1: Find out what the value actually is
Before anything else, log it:
console.log(user.hobbies); // undefined → now you know
console.log(user); // see what properties really exist
This often reveals a typo or a wrong path immediately.
Fix 2: Give it a default empty array
If the value might legitimately be missing, fall back to []:
const hobbies = user.hobbies || [];
const upper = hobbies.map(h => h.toUpperCase()); // works, returns []
Fix 3: Use optional chaining (?.)
const upper = user.hobbies?.map(h => h.toUpperCase());
// If hobbies is undefined, the result is undefined instead of crashing
You can combine it with a default:
const upper = user.hobbies?.map(h => h.toUpperCase()) ?? [];
The very common React version
function UserList() {
const [users, setUsers] = useState(); // ❌ starts as undefined
useEffect(() => {
fetch("/api/users")
.then(res => res.json())
.then(data => setUsers(data));
}, []);
return (
<ul>
{users.map(u => <li key={u.id}>{u.name}</li>)} {/* 💥 crashes on first render */}
</ul>
);
}
On the first render the fetch hasn't finished, so users is undefined.
Fix: start with an empty array.
const [users, setUsers] = useState([]); // ✅ starts as an empty array
Now the first render maps over [] (shows nothing), and when the data arrives it re-renders with the real list.
Also double-check the shape of the response. If the API returns { users: [...] }, you need:
.then(data => setUsers(data.users));
Quick checklist when you see this error
- Look at the line number in the error and find the
.map.
console.log whatever is before .map.
- Ask: Is it misspelled? Has the data loaded yet? Is the data shaped the way I think?
- Add a safe default (
[], || [], or ?.) where the value can legitimately be missing.
The same idea applies to similar errors like reading 'length' or reading 'name'. The thing before the dot is undefined.