error: invalid input syntax for type uuid: "undefined"

Node.js · updated 2026-11-10

Raised by Postgres (SQLSTATE 22P02) when a value compared with or stored in a uuid column cannot be parsed as a UUID. The quoted value is the giveaway: "undefined" or "null" means JavaScript turned a missing variable into text, a number means a route meant for numeric ids. node-postgres throws it, psycopg raises InvalidTextRepresentation.

error: invalid input syntax for type uuid: "undefined"
    at node_modules/pg/lib/client.js:694:17
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
  code: '22P02'

Quick fix

Look at the quoted value. "undefined" or "null" means the id variable was missing and got turned into a string, usually a route parameter or a property name that does not match. Validate ids at the edge (a UUID check before the query) and answer 400 or 404, so bad ids never reach the database.

  1. Read the quoted value in the message.
  2. "undefined" or "null": find where the id is built. A template string or String() of a missing value produces that text.
  3. A number: the URL or client is using an old numeric id against a uuid column.
  4. Validate before querying: reject anything that is not a UUID with a 400 (or a 404 for a lookup by id).
  5. Pass ids as query parameters, never by building SQL strings.
typescript
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

app.get('/orders/:id', async (req, res) => {
  const { id } = req.params;
  if (!UUID.test(id)) return res.status(404).json({ error: 'Order not found' });
  const { rows } = await pool.query('select * from orders where id = $1', [id]);
  // ...
});

Why it happens

A missing variable turned into text

A template string or a URL built from an undefined id sends the literal string "undefined" (or "null").

Route parameter name mismatch

The route defines :orderId but the handler reads req.params.id, so the id is undefined.

Old numeric ids

A table moved from serial ids to UUIDs while links, clients or caches still use numbers.

Unvalidated user input

Any string from the URL reaches the query; bots probing /orders/1 produce a steady trickle of these.

Other fixes

  • Report these with the request path attached: a burst right after a deploy usually means a broken link or route, a steady trickle is usually bots.
  • The same 22P02 code covers other types, for example invalid input syntax for type integer: "abc".

Same error, other wording

  • error: invalid input syntax for type uuid: "null"
  • psycopg.errors.InvalidTextRepresentation: invalid input syntax for type uuid: "42"
  • {"code":"22P02","message":"invalid input syntax for type uuid: \"undefined\""}

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, 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 "invalid input syntax for type uuid" errors, show me the quoted values and the routes they came from, fix the code that builds "undefined" ids, and add UUID validation at those routes.

Setup for Node.js and Express or Vercel Functions, then have your agent fix and resolve it.

Related errors

More in Databases

References: PostgreSQL docs: the uuid type and its accepted input forms, PostgreSQL docs: error codes (22P02 invalid_text_representation)

Spotted a mistake? Email hello@greatwork.company. About Runheed.