After optional chaining (`?.`) returns undefined, the code tries to call a method on it, throwing TypeError.
After using optional chaining (`?.`) to safely access a potentially-null property, the code immediately tries to call a method on it without checking the result. Since `?.` returns `undefined` (not an error) when the chain breaks, the subsequent `.map()`, `.filter()`, `.split()` etc. throws `TypeError: Cannot read property ‘x’ of undefined`. This is the most common runtime error in JavaScript and often surfaces after API changes or schema migrations.
// VULNERABLE const items = data.items; // could be undefined if API response changed const names = items.map(item => item.name); // TypeError if items is undefined
// FIXED — optional chaining throughout the chain const names = data.items?.map(item => item.name) ?? [];
pool.query(`SELECT * FROM users WHERE id = ${req.params.id}`)