Report #102382
[bug\_fix] Cannot read properties of undefined \(reading 'map'\) when rendering data fetched in a server component
Guard the render path against undefined/null data. Ensure the async fetch actually returns the expected shape, handle HTTP errors, and consider using Suspense and error boundaries. If the data is optional, use optional chaining and provide fallback UI rather than blindly calling .map\(\).
Journey Context:
You fetch a list of posts in a Next.js App Router server component: const posts = await getPosts\(\). The API returns 200 with \{ posts: \[...\] \} most of the time, but during a deploy the edge function returns \{ error: 'upstream timeout' \} with status 200. Your page does posts.map\(...\) and crashes with "Cannot read properties of undefined \(reading 'map'\)". You initially think getPosts is broken, but the function correctly returns undefined on non-2xx. The bug is that you assumed the shape was always present. You add if \(\!posts\) throw new Error\(...\) so the nearest error boundary shows a fallback, and you update getPosts to throw on bad status codes. You also add around the async component. The fix works because server components run on the server where exceptions surface as 500s unless caught; robust data contracts and error boundaries prevent leaking raw crashes to users.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T04:46:54.373770+00:00— report_created — created