{"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":166,"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":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/brianc/node-postgres/blob/c5e8c9a57bff6d9160ec5dbd5c4f4c1e4c460711/packages/pg/lib/native/client.js#L148-L184","documentation":"The native (pg-native-backed) Client.query() throws synchronously as a TypeError when the first argument is null or undefined (native/client.js:165-166). This is an explicit guard before any NativeQuery construction at line 176. Note: passing { text: null } would NOT trigger this — only the first arg itself being null/undefined does.","triggerScenarios":"Calling client.query(null), client.query(undefined), or client.query(someVar) where someVar resolved to null/undefined at runtime. The check at line 165 is a strict === comparison.","commonSituations":"A SQL builder function returns undefined for an edge case; an optional parameter destructured from a config object that was absent; a refactor that moved the query text out of the call site without a default.","solutions":["Default the query variable before calling: `const sql = buildSql() ?? 'SELECT 1'`.","Guard at the call site: `if (!sql) return`.","Type the parameter as string (not string|undefined) so TypeScript rejects the call at compile time."],"exampleFix":"// before\nconst sql = maybeBuildQuery(opts) // can return undefined\nawait client.query(sql)\n// after\nconst sql = maybeBuildQuery(opts)\nif (sql === null || sql === undefined) {\n  throw new TypeError('query builder returned no SQL')\n}\nawait client.query(sql)","handlingStrategy":"validation","validationCode":"const sql = maybeBuildQuery(opts)\nif (sql === null || sql === undefined) {\n  throw new TypeError('Cannot execute query: SQL config is null/undefined')\n}\nawait client.query(sql)","typeGuard":"function isQueryableConfig(c): c is string | object {\n  return c !== null && c !== undefined\n}\n\nif (!isQueryableConfig(config)) {\n  throw new TypeError('client.query requires a string or config object')\n}\nawait client.query(config)","tryCatchPattern":"try {\n  await client.query(maybeSql)\n} catch (err) {\n  if (err instanceof TypeError && /null or undefined query/.test(err.message)) {\n    logger.warn('skipped query: caller passed null/undefined SQL')\n    return null\n  }\n  throw err\n}","preventionTips":["Type query text as string, not string|undefined, so the compiler rejects bad calls.","Default optional query params with ?? at the boundary.","Assert truthy SQL at the output of any query-builder function."],"tags":["query","validation","native","typescript"],"analyzedSha":"c5e8c9a57bff6d9160ec5dbd5c4f4c1e4c460711","analyzedAt":"2026-08-03T18:47:28.334Z","schemaVersion":2}