{"record":{"id":"30d23418be548ab1","repo":"brianc/node-postgres","slug":"client-was-passed-a-null-or-undefined-query-30d234","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/native/client.js","lineNumber":168,"sourceCode":"// send a query to the server\n// this method is highly overloaded to take\n// 1) string query, optional array of parameters, optional function callback\n// 2) object query with {\n//    string query\n//    optional array values,\n//    optional function callback instead of as a separate parameter\n//    optional string name to name & cache the query plan\n//    optional string rowMode = 'array' for an array of results\n//  }\nClient.prototype.query = function (config, values, callback) {\n  let query\n  let result\n  let readTimeout\n  let readTimeoutTimer\n  let queryCallback\n\n  if (config === null || config === undefined) {\n    throw new TypeError('Client was passed a null or undefined query')\n  } else if (typeof config.submit === 'function') {\n    readTimeout = config.query_timeout || this.connectionParameters.query_timeout\n    result = query = config\n    // accept query(new Query(...), (err, res) => { }) style\n    if (typeof values === 'function') {\n      config.callback = values\n    }\n  } else {\n    readTimeout = config.query_timeout || this.connectionParameters.query_timeout\n    query = new NativeQuery(config, values, callback)\n    if (!query.callback) {\n      let resolveOut, rejectOut\n      result = new this._Promise((resolve, reject) => {\n        resolveOut = resolve\n        rejectOut = reject\n      }).catch((err) => {\n        Error.captureStackTrace(err)\n        throw err","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/brianc/node-postgres/blob/ff9d775abd12f29dd6df03945253b54eabbb29f2/packages/pg/lib/native/client.js#L150-L186","documentation":"A TypeError thrown synchronously by the native Client's query() method when the first argument (config) is null or undefined. The method accepts a string, an object, or a Query instance as the first argument — but explicitly rejects null and undefined to fail fast rather than producing a confusing downstream error.","triggerScenarios":"Calling nativeClient.query(null) or nativeClient.query(undefined). This happens when a variable intended to hold the query text or config object was never assigned, conditionally set to null, or came from an object property that doesn't exist (e.g., req.body.query when the body has no query field). The check is at native/client.js:167-168.","commonSituations":"Building a query string from a variable that was conditionally set and the condition wasn't met; destructuring a config object where a property is missing; passing an expression like req.body.sortBy that evaluates to undefined; refactoring that accidentally passes the wrong variable.","solutions":["Ensure the first argument to query() is always a string, object, or Query instance — never null or undefined.","Add a guard clause before calling query() to check the argument is defined.","If the query text comes from user input, validate it exists and is a non-empty string before passing.","Use TypeScript to catch null/undefined at compile time with strict null checks."],"exampleFix":"// before — queryText may be undefined\nconst queryText = req.body.query // could be undefined\nawait client.query(queryText)\n\n// after — guard before calling\nconst queryText = req.body.query\nif (!queryText) {\n  throw new Error('Query text is required')\n}\nawait client.query(queryText)","handlingStrategy":"validation","validationCode":"function safeQuery(client, config, values, callback) {\n  if (config === null || config === undefined) {\n    throw new TypeError('Query config is required — received ' + String(config))\n  }\n  return client.query(config, values, callback)\n}","typeGuard":"// TypeScript: narrow the query input type\nfunction isValidQueryConfig(config: unknown): config is string | object {\n  return config !== null && config !== undefined && (typeof config === 'string' || typeof config === 'object')\n}\n\n// Usage:\nif (!isValidQueryConfig(queryInput)) {\n  throw new Error('Invalid query input')\n}\nawait client.query(queryInput)","tryCatchPattern":"try {\n  await client.query(config)\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('null or undefined query')) {\n    throw new Error('Query was called without a valid config — check the caller provides a query string or object')\n  }\n  throw err\n}","preventionTips":["Always validate that query text or config exists before calling client.query().","Use TypeScript with strict null checks to catch null/undefined at compile time.","When building queries from user input, verify the input field exists and is non-empty.","Add default values when destructuring config objects: const { query = '' } = config."],"tags":["query","native","typeerror","programming-error","validation"],"backgroundTag":null,"analyzedSha":"ff9d775abd12f29dd6df03945253b54eabbb29f2","analyzedAt":"2026-08-11T15:33:59.644Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}