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.
- Read the quoted value in the message.
- "undefined" or "null": find where the id is built. A template string or String() of a missing value produces that text.
- A number: the URL or client is using an old numeric id against a uuid column.
- Validate before querying: reject anything that is not a UUID with a 400 (or a 404 for a lookup by id).
- Pass ids as query parameters, never by building SQL strings.
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
- error: insert or update on table "orders" violates foreign key constraint...
- TypeError: Cannot read properties of undefined (reading 'x')
- error: relation "todo" does not exist
More in Databases
- PrismaClientKnownRequestError: Unique constraint failed on the fields: (`email`)Node.js
- PrismaClientInitializationError: Can't reach database server at `db.example.com:5432`Node.js
- MongoServerError: E11000 duplicate key error collection: app.users index: email_1 dup...Node.js
- [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379Node.js
- psycopg2.OperationalError: server closed the connection unexpectedlyPython
- django.db.utils.OperationalError: no such table: orders_orderPython
- psycopg.errors.UniqueViolation: duplicate key value violates unique constraint...Python
- error: new row violates row-level security policy for table "todos"Node.js
- psycopg.errors.UndefinedColumn: column "titel" does not existPython
- error: infinite recursion detected in policy for relation "members"Node.js
- error: permission denied for table profilesNode.js
- error: null value in column "total" of relation "orders" violates not-null constraintNode.js
- sqlalchemy.exc.PendingRollbackError: This Session's transaction has been rolled back due...Python
- sqlite3.OperationalError: database is lockedPython
- pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection...Python
- redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection...Python
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.