TypeError: data.map is not a function
TypeError: data.map is not a functionIn one sentence: You called .map() on something that isn't an array, usually an API response object, undefined, or a value that hasn't loaded yet.
What it means
`.map()` only exists on arrays. If the variable holds an object, a string, `null` or `undefined`, JavaScript throws this error the moment your component tries to render the list.
With AI-generated code this usually means the API returns `{ items: [...] }` (or `{ data: [...] }`) but the component assumes it gets the array directly.
Common causes
- The API wraps the list in an object, like `{ results: [...] }`, and the code maps over the whole object.
- State starts as `null`, `undefined` or `{}` instead of an empty array `[]`.
- The request failed and the error body (an object) was stored where the list should be.
- The value is a JSON string that was never parsed.
How to fix it
- Log the value right before `.map()` (`console.log(data)`) and look at its real shape.
- If the array is inside a property, map over that property: `data.items.map(...)`.
- Initialize list state as an empty array: `useState<Item[]>([])`.
- Guard the render while data loads or after an error: `Array.isArray(data) && data.map(...)`.
Before
const [posts, setPosts] = useState(null);
// API returns { posts: [...] }
setPosts(await res.json());
return posts.map(p => <Post key={p.id} {...p} />);After
const [posts, setPosts] = useState<Post[]>([]);
const json = await res.json();
setPosts(Array.isArray(json.posts) ? json.posts : []);
return posts.map(p => <Post key={p.id} {...p} />);Tried this and it's still broken? Paste your exact error for a free explanation of your specific case.
Prompt to paste into your AI tool
My app throws 'TypeError: data.map is not a function'. Check what shape the API actually returns, map over the correct array property, initialize the state as an empty array, and guard the render while loading or on error. Don't change unrelated code.Still stuck?
Paste your exact error and code. Get a plain-English explanation free, or the full fix for $5.
Hitting errors every day? Builder is $15/mo for 100 fixes per month. Compare plans
Related errors
- TypeError: Failed to fetch
- Supabase error: JWT expired
- Error: Dynamic server usage: Route couldn't be rendered statically because it used cookies
- Module not found: Can't resolve 'fs'
Weekly: the errors breaking vibe-coded apps and how to fix them. Free.
No spam. Unsubscribe anytime.