Webhook data is nested under .body, not at the root.This is the most common Code node mistake.
// ❌ WRONG — returns undefinedconst name = $json.name;const email = $json.email;// ✅ CORRECT — webhook data is under .bodyconst name = $json.body.name;const email = $json.body.email;// Also correct using $inputconst webhookData = $input.first().json.body;const name = webhookData.name;
Best for: aggregation, filtering, batch processing, transformations
Faster for multiple items (single execution)
// Example: Calculate total from all itemsconst allItems = $input.all();const total = allItems.reduce((sum, item) => sum + (item.json.amount || 0), 0);return [{ json: { total, count: allItems.length, average: total / allItems.length }}];
When to use: Comparing items across the dataset, calculating totals, averages, statistics, sorting or ranking, deduplication, and building aggregated reports.
Use for specialized cases only.
Code executes separately for each input item
Access data via $input.item or $item
Best for: per-item validation with different error handling
Slower for large datasets
// Example: Add processing timestamp to each itemconst item = $input.item;return [{ json: { ...item.json, processed: true, processedAt: new Date().toISOString() }}];
When to use:
Each item needs an independent API call
Item-specific transformations based on item properties
When items must be processed separately for business reasons
Decision shortcut: Need to look at multiple items? Use “All Items”. Each item completely independent? Use “Each Item”. Not sure? Use “All Items”.
// ❌ WRONG: No return statementconst items = $input.all();const total = items.reduce((sum, i) => sum + i.json.amount, 0);// Forgot to return!// ✅ CORRECT: Always returnconst items = $input.all();const total = items.reduce((sum, i) => sum + i.json.amount, 0);return [{json: {total}}];
#2: Using n8n expression syntax in code
// ❌ WRONG: {{ }} is for expression fields, not Code nodesconst value = "{{ $json.field }}";const name = '={{$json.body.name}}';// ✅ CORRECT: Direct JavaScript accessconst value = $json.field;const name = $json.body.name;// ✅ ALSO CORRECT: Template literal (for interpolation)const message = `Hello ${$json.body.name}!`;
// ❌ WRONG: Crashes if user or email doesn't existconst email = item.json.user.email;// ✅ CORRECT: Optional chaining with defaultconst email = item.json?.user?.email || 'no-email@example.com';// ✅ ALSO CORRECT: Explicit guard clauseif (!item.json.user) { return [];}const email = item.json.user.email;
#5: Webhook body nesting
// ❌ WRONG: Webhook data is not at $json rootconst email = $json.email;const name = $json.name;// ✅ CORRECT: Webhook data is under .bodyconst email = $json.body.email;const name = $json.body.name;// ✅ ALSO CORRECT: Using $inputconst body = $input.first().json.body;const email = body.email;