← Back to Case Library

Cases API: Unhandled TypeError on undefined rows in CaseController.submit

DomainQUERYKEY ImpactMedium
Error Signature
querykey:cases-api:unhandled-rejection-cannot-read-properties

db.js query() returns undefined during pool exhaustion causing CaseController.submit to access .id on undefined.

Root Cause

The db.js query helper destructures pool.execute() result with const [rows] = result. When the MariaDB pool is exhausted, execute() yields [undefined, fields], making rows undefined. Downstream code in CaseController.submit accesses rows[0].id without a null guard, producing an unhandled TypeError in Express 4.x async context.

Solution Steps

  1. In db.js query() helper: after pool.execute(), add a guard: if (!Array.isArray(rows)) return [] — handles pool exhaustion where execute() returns [undefined, fields] rather than [Array, fields].
  2. In CaseController.submit: add null guard before accessing rows[0].id. Validate rows is a non-empty array and return 404 if not, preventing the unhandled TypeError in the Express 4.x async context.

Validation

[object Object]

Ecosystem: querykey

License: CC-BY-4.0

Last updated: May 21, 2026

Case ID: daadfb64-d589-446f-9e73-543fa79ec837