{"id":"60a4157317a4082a","repo":"brianc/node-postgres","slug":"client-was-passed-a-null-or-undefined-query","errorCode":null,"errorMessage":"Client was passed a null or undefined query","messagePattern":"Client was passed a null or undefined query","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/pg/lib/client.js","lineNumber":632,"sourceCode":"            activeQuery.handleError(queryError, this.connection)\n            this.readyForQuery = true\n            this._pulseQueryQueue()\n          })\n        }\n      } else if (this.hasExecuted) {\n        this._activeQuery = null\n        this.emit('drain')\n      }\n    }\n  }\n\n  query(config, values, callback) {\n    // can take in strings, config object or query object\n    let query\n    let result\n\n    if (config == null) {\n      throw new TypeError('Client was passed a null or undefined query')\n    }\n\n    if (typeof config.submit === 'function') {\n      result = query = config\n      if (!query.callback) {\n        if (typeof values === 'function') {\n          query.callback = values\n        } else if (callback) {\n          query.callback = callback\n        }\n      }\n    } else {\n      query = new Query(config, values, callback)\n      if (!query.callback) {\n        result = new this._Promise((resolve, reject) => {\n          query.callback = (err, res) => (err ? reject(err) : resolve(res))\n        }).catch((err) => {\n          // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the","sourceCodeStart":614,"sourceCodeEnd":650,"githubUrl":"https://github.com/brianc/node-postgres/blob/c5e8c9a57bff6d9160ec5dbd5c4f4c1e4c460711/packages/pg/lib/client.js#L614-L650","documentation":"Thrown synchronously (as a TypeError) by Client.prototype.query when the first argument (config) is null or undefined. The guard at client.js:631-633 uses a loose null check (config == null catches both null and undefined) because every query invocation requires at least a query config, text string, or Query object. Passing nothing or an explicitly null value means the caller has a logic error upstream.","triggerScenarios":"client.query(null), client.query(undefined), or client.query(someVariable) where someVariable was never assigned. This throws synchronously in the calling stack frame, not as a rejected promise.","commonSituations":"A query string is built conditionally and the variable is undefined when the branch is skipped. A function parameter defaults to undefined and is forwarded to query(). Destructuring from an object where the text property is missing.","solutions":["Ensure the query config/text argument is always a non-null value before calling client.query().","Add a default or guard: const sql = buildQuery() ?? 'SELECT 1'; client.query(sql).","If using TypeScript, enable strictNullChecks so the type system catches undefined at compile time."],"exampleFix":"// before\nconst sql = maybeBuildQuery(); // may return undefined\nawait client.query(sql);\n\n// after\nconst sql = maybeBuildQuery();\nif (!sql) throw new Error('Query text is required');\nawait client.query(sql);","handlingStrategy":"type-guard","validationCode":"function safeQuery(client, config, values, callback) {\n  if (config == null) {\n    throw new TypeError('query config/text must not be null or undefined');\n  }\n  return client.query(config, values, callback);\n}","typeGuard":"function isValidQueryConfig(config) {\n  return config != null && (typeof config === 'string' || typeof config === 'object');\n}\n\n// usage:\nif (isValidQueryConfig(sql)) {\n  client.query(sql);\n}","tryCatchPattern":"try {\n  await client.query(sql);\n} catch (err) {\n  if (err instanceof TypeError && /null or undefined query/i.test(err.message)) {\n    console.error('Query was not built — check upstream logic');\n  } else {\n    throw err;\n  }\n}","preventionTips":["Always validate that a query variable is defined before passing it to client.query().","Use TypeScript with strictNullChecks to catch undefined at compile time.","Avoid conditionally assigning query text without a fallback or guard."],"tags":["query","validation","typeerror"],"analyzedSha":"c5e8c9a57bff6d9160ec5dbd5c4f4c1e4c460711","analyzedAt":"2026-08-03T18:47:28.334Z","schemaVersion":2}