This TypeError means code tried to read a property from undefined. Find why the value was missing before adding guards everywhere.
Symptoms
- Console shows Cannot read properties of undefined.
- The page fails only before data loads.
- The error mentions map, length, id or a nested field.
Likely causes
- API response shape changed.
- Async state is read before loading finishes.
- Selector or query result is missing.
- An array item does not contain the expected object.
Fix steps
- Log the value immediately before the failing line.
- Validate API response shape.
- Add loading and empty states.
- Use optional chaining only after understanding the missing data path.
Verify the fix
- Test empty, loading and error API responses.
- Check browser console after reload.
- Add a small unit or UI test for missing fields when possible.
FAQ
Should I just add optional chaining?
It can prevent crashes, but first confirm whether missing data is expected or a real bug.
Why does it happen only sometimes?
Timing, cached data and API shape differences often make async bugs intermittent.
Related tools and guides
Last updated: May 18, 2026