DEV Community

Applied Efficiency
Applied Efficiency

Posted on

Cannot read properties of undefined? Your screen rendered before your data arrived

Your AI-built app crashes with TypeError: Cannot read properties of undefined (reading 'streak') — and the code looks fine.

What it means: some variable your code expected to hold data was empty when the code ran. In AI-built apps, this is usually a timing problem: the screen renders before the data arrives, and the code tries to read the data too early.

Why it happens:

  • Data loads asynchronously, but the UI renders immediately
  • An API returned an empty or differently-shaped response than the AI assumed
  • A prop or state value was never set on this code path

The fix:

  1. Find the line in the error — the first file/line in the stack that's yours.
  2. Add a guard: show a loading state until the data exists — if (!habit) return <p>Loading…</p>.
  3. Log the actual API response once. AI-written code often assumes a shape the API doesn't return.

Paste this into your AI:

"I'm getting 'Cannot read properties of undefined'. The data probably hasn't loaded when this renders. Add a loading guard, and show me what shape of data this component actually receives."

This is Debug Clinic pattern #4 in Last20 Academy — the five errors every vibe coder hits, each with the fix and the exact prompt to paste. Try it free: https://last20.appliedefficiencyai.com/kit

Top comments (0)