| Level Navigation: 1 | 2 | 3 | (4ℹ️) | (5ℹ️) | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14⚡ | 15⚡ | (16ℹ️) | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26⚡ | 27⚡ | 28⚡ | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39⚡ | 40⚡ |
400 plus an error message when data is missing.items fields.
if (!req.body?.title) {
return res.status(400).json({ error: 'Title is required' });
}
Understanding the shorthand: !req.body?.title uses two JavaScript features:
?.): Safely accesses title even if req.body is undefined or null. Without ?, accessing req.body.title when req.body is undefined would throw an error.!): Negates the value, so the condition is true when title is missing, empty string, null, undefined, 0, or false.This is equivalent to: if (!req.body || !req.body.title) but more concise and safer.