TypeError: Cannot read properties of undefined (reading 'x') means the code did something like user.name when user was undefined (or null). The thing before the dot isn't there. The most common reasons: data from an API hadn't arrived yet, or arrived in a different shape than expected; the code looked for an element that isn't on the page; or a value was never set. The name in brackets is the property it tried to read, which helps you find the line.
What's the problem
The console shows Cannot read properties of undefined (reading '...') or Cannot read property '...' of null, and part of the page breaks or goes blank.
Why it happens
- Data not loaded yet: the page renders before an API response arrives.
- The API returned something different: an error, an empty list, or a renamed field.
- An element isn't on the page: the code looks for something by ID or class that doesn't exist on this page, or runs before the page has loaded.
- A missing value: an optional field that's sometimes empty.
How to fix it
- Click the file and line in the console to find the exact code.
- Find what's undefined: the object before
.property. - If it's API data, check the response in the Network tab: did it fail, or is the shape different?
- Guard against missing values: check before reading, or use optional chaining (
user?.name). - For page elements, make sure the code runs after the page loads, and only on pages that have the element.
- Fix the cause too, not just the symptom: if the API is failing, the guard only hides it.
What Preventionlabs checks
One click runs the full report on your domain. For this problem it shows whether your site loads, how fast, and where its redirects go; script libraries on your homepage with published security holes; every outside service your homepage loads scripts and files from. With Cloudflare connected, it also shows recent deploys and account changes, with their times. It doesn't run your pages in a browser, so open the browser console on the failing page to see the error itself.
Run your free reportYour first report is free, no card needed. Then A$50 a month, up to 10 domains. Read-only: it never changes anything.
Sources
- MDN Web Docs: TypeError: "x" is (not) "y"reference
Cannot read properties of undefined (reading 'x') (V8-based)
- MDN Web Docs: TypeError: "x" is (not) "y"reference
Oftentimes, unexpected undefined or null values.