Skip to content

JavaScript errorTypeError: “Cannot read properties of undefined (reading 'x')”

The page goes blank or a function stops with TypeError: Cannot read properties of undefined (reading 'name'), or the same with null. The code asked for a property on something that does not exist yet. The word in brackets is the property it tried to read; the value before the dot is the one that was undefined or null.

Short answer

Open the stack trace at the first line in your own code and look at what comes before .name on that line: that value is missing. Usually the data has not loaded yet (the first render runs before the fetch finishes), the API returned a different shape than the code expects, or an array index or find() came back empty. Guard it with optional chaining (user?.name), a default (data ?? []), or an early return until the data is there. Firefox says can't access property "name", user is undefined, and Safari says undefined is not an object (evaluating 'user.name'), for the same problem.

Why the value is missing, and the fix

  1. 01

    The data has not loaded yet

    “You cannot invoke a method on an undefined or null variable.” MDN, TypeError: "x" is (not) "y"

    React, Vue and other UI code render once before a fetch finishes. If the initial state is undefined, the first render reads a property of nothing. Start with a value of the right shape, or render a loader until the data arrives.

    const [user, setUser] = useState(null);
    
    if (!user) return <Spinner />;
    return <h1>{user.name}</h1>;
    
    // or, for a single read
    <h1>{user?.name}</h1>
  2. 02

    The API response has a different shape

    “To fix null pointer to undefined or null values, you can test if the value is undefined or null first.” MDN, TypeError: "x" is (not) "y"

    The code expects data.user.name, but the API sends data.name, wraps it in data.data, or leaves the field out for some records. Log the response once (console.log(JSON.stringify(data, null, 2))) and match the code to what actually arrives. Treat fields the API does not guarantee as optional.

    const city = order?.shipping?.address?.city ?? 'Unknown';
  3. 03

    An empty lookup: find(), an index or a missing key

    array.find() returns undefined when nothing matches, items[0] is undefined on an empty array, and obj[key] is undefined for a key that is not there. Check the result before reading from it, especially for data that depends on the URL, a filter or user input.

    const product = products.find((p) => p.id === id);
    if (!product) return notFound();
  4. 04

    A DOM element that is not there

    document.querySelector() returns null when the element does not exist yet or the selector is wrong, so reading .value or calling .addEventListener on it fails with “Cannot read properties of null (reading 'value')”. Run the code after the element exists (a script with defer, or inside a framework’s mount hook), and check the selector.

Checked against the vendor’s docs on : MDN: TypeError: "x" is (not) "y" (Cannot read properties of undefined).

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: TypeError: “x is not a function” · “Unexpected token < in JSON” · Vercel environment variables undefined

Questions

More on this error

Why does it work locally but crash in production?

Production data is different: real records with missing fields, slower networks that expose the first empty render, or an environment variable that is undefined on the host. Find the value before the dot in the production stack trace (source maps help) and guard that one.

Should I put ?. everywhere?

No. Use it where a missing value is normal, such as optional API fields or data that is still loading. Where a value must exist, a missing one is a real bug, and hiding it with ?. just moves the failure somewhere harder to find.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.