Skip to content

JavaScript errorTypeError: “Cannot destructure property 'x' of 'y' as it is undefined”

Code like const { data } = result or function Card({ title }) fails with TypeError: Cannot destructure property 'data' of 'result' as it is undefined (or as it is null). Destructuring reads properties from the value on the right, and that value does not exist yet.

Short answer

Find what is undefined: the name after of in the message. For a function parameter the message names 'undefined' itself, so look at the call site. If it is a function parameter, the function was called without that argument: give it a default, function f({ a } = {}). If it is data from an API, a hook or a database, it has not loaded or the field is missing: check it first, or use const { a } = value ?? {}. Note that a default value does not apply when the value is null, only when it is undefined.

Why there is nothing to destructure, and the fix

  1. 01

    A function called without its object argument

    “Same as accessing properties, destructuring null or undefined throws a TypeError.” MDN, Destructuring

    A function that destructures its parameter fails when someone calls it with no argument, or with undefined. Give the parameter an empty object as its default.

    function createUser({ name, role = 'member' } = {}) {
      return { name, role };
    }
    createUser(); // no error
  2. 02

    Data that has not loaded or is missing

    The first render before a fetch finishes, an API response without the expected field, or a hook or context used outside its provider all give undefined. Handle the empty state before destructuring.

    const json = await res.json();
    const { user } = json;
    if (!user) throw new Error('Response has no user');
    const { id, email } = user;
  3. 03

    null, where defaults do not help

    “It is not used if the property has value null.” MDN, Destructuring: default value

    Databases and APIs often return null for a missing record. A default in the pattern only covers undefined, so null still throws. Use ?? to swap null for an object.

    const { name } = (await findUser(id)) ?? {};
  4. 04

    React props and hooks

    A component destructuring a prop object that a parent did not pass, or useContext() returning undefined outside the provider (when createContext was given no default), fails the same way. Pass the prop, give it a default, or check that the provider wraps the tree.

    function Profile({ user = {} }) {
      const { name = 'Guest' } = user;
      return <p>{name}</p>;
    }

Checked against the vendor’s docs on : MDN: Destructuring.

Rather hand it over?

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

Related: “Cannot read properties of undefined” · React “Invalid hook call” · JavaScript “Invalid Date”

Questions

More on this error

Is this the same as “Cannot read properties of undefined”?

Same cause, different syntax. Reading obj.x on undefined gives Cannot read properties; destructuring { x } from undefined gives Cannot destructure. The fixes are the same.

Should I use optional chaining instead?

For one value, value?.x is often clearer than destructuring with a fallback. Use whichever reads better, but decide what the code should do when the data is missing.

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.