Error explained
TypeError: Cannot read properties of undefined
Your code accessed a property on something that was undefined. The fix is to find why the value was missing, not just to hide the error.
3 min read8 sectionsUpdated
On this page
Key points
- The message names the property: "(reading 'map')" means something.map where something was undefined.
- Async data not loaded yet is the most common cause in front-end code.
- Optional chaining and defaults prevent the crash; validation prevents the bad data.
- Source maps turn minified stack traces into your real file and line.
| Cause | Example | Fix |
|---|---|---|
| Data not loaded yet | user.profile.name before the fetch resolves | Render a loading state; user?.profile?.name |
| API field missing or renamed | order.items.map when items is absent | Validate responses; default to [] |
| Wrong destructuring | const { data } = await res (forgot .json()) | Check the shape you actually get |
| Array index out of range | rows[0].id on an empty array | Check length first |
| this lost in callbacks | this.state in a plain function callback | Arrow functions or bind |
| Optional config not set | config.api.url when api is missing | Validate config at startup |
// optional chaining and nullish defaults
const name = user?.profile?.name ?? 'Guest';
const total = (order.items ?? []).reduce((s, i) => s + i.price, 0);
// validate external data once, at the boundary
import { z } from 'zod';
const Order = z.object({ id: z.string(), items: z.array(z.object({ price: z.number() })) });
const order = Order.parse(await res.json()); // throws a clear error if the shape is wrongDebugging it
Read the stack trace from the top: the first frame in your own code is where the access happened. In production bundles the trace is minified (a.b is undefined at main.4f2c.js:1:28311); upload source maps per release so your error tracker shows the original file and line. Then ask why the value was undefined: log or inspect the object one level up.
Catching it in production
In the browser, real user monitoring records JavaScript errors from real visitors; with source maps uploaded, the stack points to your source. Watch for a new error appearing right after a release.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
Is "Cannot read property of undefined" the same error?
Yes. Older browsers and Node versions used that wording; newer ones say "Cannot read properties of undefined (reading 'x')".
Should I wrap everything in try/catch?
No. Fix the data flow and validate inputs. Catch errors at boundaries, such as an error boundary or a request handler, and report them.
Related reading
- What is frontend monitoring?: Frontend monitoring explained: Core Web Vitals, JavaScript errors, failed API calls, session replay and synthetic checks, and how to set it up for a web app.
- Error: read ECONNRESET: What read ECONNRESET means, common causes such as keep-alive timeout mismatches, proxies and crashing servers, how to find which side reset, and safe retries.
- Nginx "upstream timed out (110)": how to fix it: Fix Nginx upstream timed out (110: Connection timed out) while reading response header: find the slow request, tune proxy timeouts and fix the backend.
- SyntaxError: Cannot use import statement outside a module: Why Node.js throws "Cannot use import statement outside a module", and the fixes: type module in package.json, .mjs files, TypeScript and Jest settings.
- Sentry vs Datadog: Sentry vs Datadog compared: developer-focused error tracking against a full observability platform, covering errors, tracing, replay, logs and cost.
- What is a flame graph?: Flame graphs explained: how stack samples become a flame graph, how to read width and depth, and the tools to produce one in each language.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.