TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.
Node.js · updated 2026-11-03
Thrown at the line with const { id } = value or at a function signature that destructures its parameter, when that value is undefined or null. The property name in the message is the first one Node tried to read.
TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.
at createOrder (file:///app/src/orders.js:12:11)
at file:///app/src/routes/orders.js:20:27Quick fix
Find what was supposed to hold the object. In Express, req.body is undefined until a body parser (express.json()) runs. For function parameters, give a default: function f({ id } = {}). For API data, check the field exists before destructuring it.
- Look at the failing line: is it a const { ... } = x, or a function whose parameter is destructured?
- Log the value just before it to see whether it is undefined or null.
- In Express, add app.use(express.json()) before the routes, and check the client sends Content-Type: application/json.
- For optional parameters, add a default object: ({ id, name } = {}).
- For nested data, use optional chaining and defaults: const { id } = res?.data ?? {}.
import express from 'express';
const app = express();
app.use(express.json()); // without this, req.body is undefined
app.post('/orders', (req, res) => {
const { productId, qty = 1 } = req.body ?? {};
if (!productId) return res.status(400).json({ error: 'productId is required' });
res.json(createOrder({ productId, qty }));
});
function createOrder({ productId, qty } = {} as { productId?: string; qty?: number }) {
// ...
}Why it happens
No body parser in Express
req.body stays undefined unless express.json() or express.urlencoded() runs first, so const { a } = req.body throws on every request.
Calling a function without its options object
function send({ to, subject }) {} throws when called as send() with no argument. A default parameter (= {}) avoids it.
A field missing from an API or database result
const { user } = await getSession() fails when getSession returns undefined for a logged-out request, or when an API changed its response shape.
Destructuring props or hook results that are not ready
In React, destructuring a prop the parent did not pass, or a hook result before data loads, gives the same error in components rendered on the server.
Other fixes
- Validate request bodies with zod so a missing body fails with a clear 400 instead of a TypeError.
- With TypeScript, mark optional parameters explicitly and let the compiler flag unchecked destructuring.
Same error, other wording
- TypeError: Cannot destructure property 'user' of 'session' as it is null.
- TypeError: Cannot destructure property 'data' of '(intermediate value)' as it is undefined.
- TypeError: Cannot read properties of undefined (reading 'id')
Let your coding agent catch the next one
Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:
> Find the 'Cannot destructure property' errors in production, show me the value that was undefined and where it came from each time, and fix the source (body parser, default parameter, or a missing-data check).
Related errors
- TypeError: Cannot read properties of undefined (reading 'x')
- TypeError: items is not iterable
- ERR_INVALID_ARG_TYPE: The "path" argument must be of type string. Received undefined
References: MDN: destructuring assignment (default values), Express docs: express.json()