
关于
在 n8n Code 节点中编写 JavaScript。适用于使用 $input/$json/$node 语法、通过 $helpers 发起 HTTP 请求、使用 DateTime 处理日期、排查 Code 节点错误或选择 Code 节点模式。
name: n8n-code-javascript description: 在 n8n Code 节点中编写 JavaScript 代码。当在 n8n 中编写 JavaScript、使用 $input/$json/$node 语法、通过 $helpers 发起 HTTP 请求、使用 DateTime 处理日期、排查 Code 节点错误或选择 Code 节点模式时使用。 risk: unknown source: community
JavaScript Code 节点
在 n8n Code 节点中编写 JavaScript 代码的专家指南。
快速开始
// Basic template for Code nodes
const items = $input.all();
// Process data
const processed = items.map(item => ({
json: {
...item.json,
processed: true,
timestamp: new Date().toISOString()
}
}));
return processed;
基本规则
- 选择 "Run Once for All Items" 模式(推荐用于大多数场景)
- 访问数据:
$input.all()、$input.first()或$input.item - 关键: 必须返回
[{json: {...}}]格式 - 关键: Webhook 数据在
$json.body下(不是直接在$json下) - 可用内置功能: $helpers.httpRequest()、DateTime (Luxon)、$jmespath()
模式选择指南
Code 节点提供两种执行模式。根据使用场景选择:
Run Once for All Items(推荐 - 默认)
适用于: 95% 的使用场景
- 工作原理: 无论输入数量多少,代码只执行一次
- 数据访问:
$input.all()或items数组 - 最适合: 聚合、过滤、批处理、转换、使用所有数据的 API 调用
- 性能: 处理多个项目时更快(单次执行)
// Example: Calculate total from all items
const 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
}
}];
适用场景:
- 跨数据集比较项目
- 计算总计、平均值或统计数据
- 排序或排名项目
- 去重
- 构建聚合报告
- 合并多个项目的数据
Run Once for Each Item
仅用于: 特殊场景
- 工作原理: 代码为每个输入项分别执行
- 数据访问:
$input.item或$item - 最适合: 项目特定逻辑、独立操作、逐项验证
- 性能: 大数据集时较慢(多次执行)
// Example: Add processing timestamp to each item
const item = $input.item;
return [{
json: {
...item.json,
processed: true,
processedAt: new Date().toISOString()
}
}];
适用场景:
- 每个项目需要独立的 API 调用
- 逐项验证且需要不同的错误处理
- 基于项目属性的特定转换
- 业务逻辑要求项目必须单独处理
快速决策:
- 需要查看多个项目? -> 使用 "All Items" 模式
- 每个项目完全独立? -> 使用 "Each Item" 模式
- 不确定? -> 使用 "All Items" 模式(可以在内部循环)
数据访问模式
模式 1: $input.all() - 最常用
适用于: 处理数组、批量操作、聚合
// Get all items from previous node
const allItems = $input.all();
// Filter, map, reduce as needed
const valid = allItems.filter(item => item.json.status === 'active');
const mapped = valid.map(item => ({
json: {
id: item.json.id,
name: item.json.name
}
}));
return mapped;
模式 2: $input.first() - 非常常用
适用于: 处理单个对象、API 响应、先进先出
// Get first item only
const firstItem = $input.first();
const data = firstItem.json;
return [{
json: {
result: processData(data),
processedAt: new Date().toISOString()
}
}];
模式 3: $input.item - 仅限 Each Item 模式
适用于: 在 "Run Once for Each Item" 模式中
// Current item in loop (Each Item mode only)
const currentItem = $input.item;
return [{
json: {
...currentItem.json,
itemProcessed: true
}
}];
模式 4: $node - 引用其他节点
适用于: 需要工作流中特定节点的数据
// Get output from specific node
const webhookData = $node["Webhook"].json;
const httpData = $node["HTTP Request"].json;
return [{
json: {
combined: {
webhook: webhookData,
api: httpData
}
}
}];
参见: DATA_ACCESS.md 获取完整指南
关键: Webhook 数据结构
最常见错误: Webhook 数据嵌套在 .body 下
// WRONG - Will return undefined
const name = $json.name;
const email = $json.email;
// CORRECT - Webhook data is under .body
const name = $json.body.name;
const email = $json.body.email;
// Or with $input
const webhookData = $input.first().json.body;
const name = webhookData.name;
原因: Webhook 节点将所有请求数据包装在 body 属性下。这包括 POST 数据、查询参数等。
兼容工具
Claude CodeCursor
标签
前端开发