Fix guides · Cursor, Lovable, Bolt, v0 ·
Fix Cannot read properties of undefined (reading 'map')
TypeError: Cannot read properties of undefined (reading 'map')
Your code calls .map() on a variable that is undefined at that moment, and .map() only exists on arrays. Usually the component renders before the data arrives, or the API response is shaped differently than expected (for example { items: [...] } instead of a plain array). The fix is to start list state as an empty array, read the right field from the response, and handle loading and error states explicitly.
Common causes – and how to tell which one you have
1. State starts as undefined
How to tell: The list comes from useState() with no starting value, or from a variable that is only filled inside useEffect or a fetch. The crash happens on the very first render, before any request finishes.
Fix: Give list state an array as its starting value, e.g. useState([]), and show a loading indicator until the request finishes.
2. API response shape doesn't match the code
How to tell: console.log the response and you see an object like { data: [...] } or { results: [...] }, while the code calls .map() on the whole response or on a field that doesn't exist. Axios wraps the body in response.data, so a JSON body of { items: [...] } ends up at response.data.items.
Fix: Read the field that actually holds the array (e.g. response.data.items) and add a TypeScript type for the response so future mismatches are flagged by the editor.
3. The request failed and the error wasn't handled
How to tell: In the browser's Network tab the request shows a 4xx/5xx status, or the body is something like { error: "Unauthorized" }. The code reads json.items from that error object and gets undefined.
Fix: Check response.ok (for fetch) or the error field (for Supabase and similar clients) before using the data, and render an error message instead of the list when the request fails.
4. Missing await on an async call
How to tell: Logging the value shows a Promise instead of data, often from response.json() or an async helper called without await. A Promise has no .items or .data field, so that lookup returns undefined.
Fix: Add await to the async call (e.g. const json = await res.json()) and make sure the surrounding function is async.
5. Prop not passed or misnamed
How to tell: The .map() is on a prop like props.items, and the parent passes it under a different name (e.g. products=) or not at all. React DevTools will show the prop is missing on that component.
Fix: Pass the prop with the exact name the child expects. A default value in the child (e.g. function List({ items = [] })) is fine for optional props, but fix the parent first.
6. Nested array missing on some items
How to tell: The crash points at an inner .map(), like post.tags.map(...) inside posts.map(...). Most records have the field but some, often older ones, don't.
Fix: Make the field always present at the source (database default or API) or handle the missing case explicitly where you render it.
How to fix it
- Open the browser console, click the first stack trace line that points to your own code, and find the exact .map() call that crashes.
- Add console.log of the variable just before that line (and of the raw API response) and reload to see what it actually contains.
- If it's undefined on first render, set the starting state to an empty array, e.g. const [items, setItems] = useState([]).
- If the log shows an object instead of an array, change the code to read the right field, e.g. setItems(json.items) or setItems(response.data.items).
- Check that every async call is awaited and that failed requests are handled (check res.ok or the error field) so the error is shown instead of crashing.
- Render a loading message while fetching and an error message if it fails, then render the list only when you have an array.
- Reload and test with data, with an empty result, and with the API failing to confirm nothing crashes.
Using Lovable, Bolt or v0?
Paste the full error and tell Lovable, Bolt or v0: "Find which .map() is crashing, log the data it receives, and fix why it's undefined. Don't just add ?. to hide it." If the data comes from Supabase, open the table in the Supabase dashboard and confirm the table and column names match what the app queries. If the app is deployed on Vercel or Netlify, check that its environment variables (API URLs, keys) are set there too, since a missing key makes requests fail and data comes back undefined.
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to open the file and line from the top of the stack trace, trace the variable back to where it is fetched or passed in, and log the real value before changing anything. Have it add a TypeScript type for the API response so the editor catches shape mismatches. To verify, ask it to check the component in four states: loading, success with data, an empty list, and a failed request. None should crash.
The “fix” to avoid
Agents often sprinkle data?.map(...) or (data || []).map(...) everywhere and call it fixed. That stops the crash but hides real bugs: a failed request or a wrong field name now just shows an empty list with no error, which is much harder to debug later. Use a safe default only for the initial loading state, and still fix the response shape and handle errors properly.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My app crashes with "TypeError: Cannot read properties of undefined (reading 'map')". Use the stack trace to find the exact .map() call, then trace that variable to its source. Check in this order: 1) list state initialised without a value (should be useState([])), 2) API response shape mismatch, e.g. reading response.items when the array is at response.data.items, 3) failed requests not handled (check res.ok or the error field), 4) a missing await on fetch/res.json(), 5) a prop passed under the wrong name. Log the real value before changing anything. Fix the root cause rather than adding ?. everywhere, add a TypeScript type for the response, and add explicit loading and error UI. Then verify the component renders correctly while loading, with data, with an empty list, and when the API returns an error.
Questions people ask
Why does it say 'undefined' and not 'null'?
The message names the value .map() was called on. If it says null instead, the variable was explicitly set to null, which Supabase does for data when a query fails, so check the error it returns.
Is adding ?. before .map a good fix?
Only as a guard during loading. If the data never arrives because of a wrong field name or a failed request, ?. just hides the bug and shows an empty page, so fix the cause too.
Why does it work after a refresh or only sometimes?
That usually means a timing issue: the component sometimes renders before the data is ready. Starting with an empty array and showing a loading state makes it consistent.