Home › JavaScript

TypeError: Cannot read properties of undefined (reading 'x') — how to fix

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

Quick fix: The thing before .map (or whatever property is named) is undefined. Give it a default (items ?? []), use optional chaining (user?.address?.city), or wait until the data is loaded.

What it means

In something.map(...), something is undefined. JavaScript can't read map from nothing. (Older browsers say Cannot read property 'map' of undefined; with null it says of null.)

Cause 1: data that isn't loaded yet (React, Vue…)

const [posts, setPosts] = useState();          // undefined on first render
useEffect(() => { fetch('/api/posts').then(r => r.json()).then(setPosts) }, []);
return posts.map(p => <Post {...p} />);        // crashes before the fetch finishes

Fix: start with a sensible empty value, or render a loading state:

const [posts, setPosts] = useState([]);
// or
if (!posts) return <p>Loading…</p>;

Cause 2: the object shape is different from what you think

const res = await fetch('/api/user').then(r => r.json());
res.user.name     // but the API returns { data: { user: ... } }

Log it and look: console.log(JSON.stringify(res, null, 2)).

Cause 3: a function that returns nothing

const getItems = () => { items.filter(i => i.active) };   // braces, no return!
getItems().length;                                       // undefined.length

Either add return or drop the braces: () => items.filter(i => i.active).

Safe access

const city = user?.address?.city;          // undefined instead of a crash
const list = (data?.items ?? []).map(fn);  // default to an empty array

Cause 4: this is undefined

In class methods passed as callbacks (onClick={this.handleClick}), this is lost. Use an arrow function class field (handleClick = () => {...}) or .bind(this).

Finding the culprit

The stack trace's first line points to the file and line. In the browser DevTools, enable Pause on exceptions in the Sources tab to inspect the variables at the moment it fails.

Last updated 2026-10-27.