You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

n8n工作流两大问题:输出膨胀与重复文件夹创建

n8n工作流两大问题解决方案

问题1:输入合并导致输出长度不匹配

「Check if name exists」Code节点接收两个输入分支:Google Sheet中的历史emoji条目(例:2条)、用户发送的新emoji名称(例:4条),期望输出4条带generatedBefore: true/false标识的结果,实际输出8条(n8n默认对多输入做笛卡尔积合并),破坏下游逻辑。

解决步骤:

  1. 配置节点执行模式:勾选「Check if name exists」节点的Run Once for All Items选项,确保节点仅执行一次,而非逐条处理输入。
  2. 修正代码逻辑:明确区分两个输入分支,使用$input.getDataAtIndex(index)分别获取不同分支的数据,替换原代码:
// 获取第一个输入分支(现有emoji列表)
const existingEmojies = $input.getDataAtIndex(0).map(item => item.json);
// 获取第二个输入分支(新emoji名称列表)
const newEmojies = $input.getDataAtIndex(1).map(item => item.json);

// 遍历新emoji,匹配现有数据生成结果
const result = newEmojies.map(emoji => {
  const exists = existingEmojies.some(e => e.emojiName === emoji.name);
  return {
    json: {
      name: emoji.name,
      generatedBefore: exists
    }
  };
});

return result;
  1. 检查连接顺序:确保「Fetch existing emojis (simulated)」连接到「Check if name exists」的输入1,「Split message into emoji array」连接到输入2,避免分支顺序错误导致数据获取异常。

问题2:Create Folder节点重复执行

工作流需每次执行仅创建一个Dropbox文件夹,实际每个emoji条目触发一次文件夹创建,生成重复文件夹。

解决步骤:

  1. 启用节点单次执行:在「Create destination folder」节点的设置中,勾选Execute Once选项,无论输入多少条目,节点仅执行一次。
  2. 可选:添加前置聚合节点:若上述方法未生效,在「Add AI prompt to each emoji」和「Create destination folder」之间新增一个Code节点,勾选「Run Once for All Items」,代码返回单个空条目,确保文件夹节点仅收到一条输入:
return [{ json: {} }];
  1. 优化文件夹命名:若使用{{ $now }}作为命名变量,建议增加时间精度(如{{ $now.format("YYYY-MM-DD-HH-mm") }}),避免短时间内重复执行生成同名文件夹(Dropbox不允许同路径下存在同名文件夹)。

修改后的完整工作流代码

{"name": "Generate emoji","nodes": [{"parameters": {},"type": "n8n-nodes-base.manualTrigger","typeVersion": 1,"position": [-848,-16],"id": "3fad7603-ec64-486e-9185-d688aad9e1e4","name": "Start (simulated Telegram trigger)"},{"parameters": {"jsCode": "return [\n{\n\"update_id\": \n444670000,\n\"message\": \n{\n\"message_id\": \n394,\n\"from\": \n{\n\"id\": \n31000000,\n\"is_bot\": \nfalse,\n\"first_name\": \n\"Afshin\",\n\"username\": \n\"afshin\",\n\"language_code\": \n\"en\"\n},\n\"chat\": \n{\n\"id\": \n-4931000000,\n\"title\": \n\"AIGEN\",\n\"type\": \n\"group\",\n\"all_members_are_administrators\": \ntrue,\n\"accepted_gift_types\": \n{\n\"unlimited_gifts\": \nfalse,\n\"limited_gifts\": \nfalse,\n\"unique_gifts\": \nfalse,\n\"premium_subscription\": \nfalse\n}\n},\n\"date\": \n1757660508,\n\"text\": \n\"Ace; King; Queen; Jack\"\n}\n}\n]"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [-656,-16],"id": "6fc6dc5e-c6a4-4372-b8a0-ae5c5a58287e","name": "Telegram message (simulated)"},{"parameters": {"conditions": {"options": {"caseSensitive": true,"leftValue": "","typeValidation": "strict","version": 2},"conditions": [{"id": "27760a34-e168-48bd-8152-d7decce12424","leftValue": "={{ $json.generatedBefore }}","rightValue": "true","operator": {"type": "boolean","operation": "true","singleValue": true}}],"combinator": "and"},"options": {}},"type": "n8n-nodes-base.if","typeVersion": 2.2,"position": [96,0],"id": "1214e355-1f7d-4843-9971-b5fec4720647","name": "Route by existence (If node)"},{"parameters": {"jsCode": "// 获取第一个输入分支(现有emoji)\nconst existingEmojies = $input.getDataAtIndex(0).map(item => item.json);\n// 获取第二个输入分支(新emoji名称)\nconst newEmojies = $input.getDataAtIndex(1).map(item => item.json);\n\nconst result = newEmojies.map(emoji => {\n  const exists = existingEmojies.some(e => e.emojiName === emoji.name);\n  return {\n    json: {\n      name: emoji.name,\n      generatedBefore: exists\n    }\n  };\n});\n\nreturn result;","executeOnce": true},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [-128,0],"id": "232a246b-6034-46fb-999d-1e6cdc23b3cb","name": "Check if name exists"},{"parameters": {"jsCode": "const items = $input.all();\nconst count = items.length;\nconst names = items.map(i => i.json.name).join('; ');\n\nconst message = `These ${count} emojis already exist and will not be generated:\n\n${names}`;\n\nreturn [{ json: { message } }];"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [320,-96],"id": "94a8d2fa-7f22-488c-8e66-450a7363e726","name": "Generate skipped emojis message"},{"parameters": {},"type": "n8n-nodes-base.noOp","typeVersion": 1,"position": [560,-96],"id": "716e0a8d-07a1-40b6-b743-14feefe4b2ca","name": "Display skipped emojis in UI"},{"parameters": {"mode": "runOnceForEachItem","jsCode": "const name = $json[\"name\"];\nconst prompt = `An emoji illustration of ${name}, isolated on a transparent background, flat vector style`;\n\nreturn {\n  json: {\n    ...$json,\n    prompt,\n  },\n};"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [320,112],"id": "a1ab5e48-9816-4ffd-886f-714ce1b9beba","name": "Add AI prompt to each emoji"},{"parameters": {},"type": "n8n-nodes-base.noOp","typeVersion": 1,"position": [624,112],"id": "39dd5b53-2f3c-4302-a010-ae48d6782872","name": "Generate emoji image by AI"},{"parameters": {"jsCode": "const items = $input.all();\nconst updatedItems = items.map((item) => {\n  const data = item?.json?.data;\n  if (data?.port > 5000) {\n    data[\"high-port\"] = true;\n  }\n  return item;\n});\nreturn updatedItems;"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [928,112],"id": "eae0c078-fb59-4875-a07d-94a400d73697","name": "Prepare upload file metadata"},{"parameters": {},"type": "n8n-nodes-base.noOp","typeVersion": 1,"position": [1168,112],"id": "c5500e71-c3ab-4f5e-adc1-cf91c190174f","name": "Upload emoji files"},{"parameters": {"authentication": "oAuth2","resource": "folder","path": "=/HotQuiz_{{ $now.format(\"YYYY-MM-DD-HH-mm\") }}"},"type": "n8n-nodes-base.dropbox","typeVersion": 1,"position": [624,368],"id": "4c0d87f1-5e4f-4dc7-905c-425eb833a26a","name": "Create destination folder","executeOnce": true,"credentials": {"dropboxOAuth2Api": {"id": "BkLHwJzXVZmXluqG","name": "Dropbox account 2"}}},{"parameters": {"jsCode": "const rawText = $json.message.text || \"\";\nconst names = rawText\n  .split(\";\")\n  .map(name => name.trim())\n  .filter(name => name.length > 0);\n\nreturn names.map(name => ({\n  json: { name }\n}));"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [-384,-160],"id": "e4d9ee77-207b-46c8-aac4-502b50149f94","name": "Split message into emoji array"},{"parameters": {"jsCode": "return [\n{\n\"row_number\": \n2,\n\"emojiName\": \n\"King\",\n\"projectName\": \n\"Ace pro\",\n\"user\": \n\"Afshin\",\n\"date\": \n\"Sep 9, 2025\",\n\"prompt\": \n\"Create an emoji of a king with cinematic lighting\"\n}\n]"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [-384,144],"id": "31004d7c-3a28-40b8-9bc1-7cb13eb6888c","name": "Fetch existing emojis (simulated)"}],"pinData": {},"connections": {"Start (simulated Telegram trigger)": {"main": [[{"node": "Telegram message (simulated)","type": "main","index": 0}]]},"Telegram message (simulated)": {"main": [[{"node": "Split message into emoji array","type": "main","index": 0}],[{"node": "Fetch existing emojis (simulated)","type": "main","index": 0}]]},"Route by existence (If node)": {"main": [[{"node": "Generate skipped emojis message","type": "main","index": 0}],[{"node": "Add AI prompt to each emoji","type": "main","index": 0}]]},"Check if name exists": {"main": [[{"node": "Route by existence (If node)","type": "main","index": 0}]]},"Generate skipped emojis message": {"main": [[{"node": "Display skipped emojis in UI","type": "main","index": 0}]]},"Add AI prompt to each emoji": {"main": [[{"node": "Generate emoji image by AI","type": "main","index": 0}],[{"node": "Create destination folder","type": "main","index": 0}]]},"Generate emoji image by AI": {"main": [[{"node": "Prepare upload file metadata","type": "main","index": 0}]]},"Prepare upload file metadata": {"main": [[{"node": "Upload emoji files","type": "main","index": 0}]]},"Create destination folder": {"main": [[{"node": "Prepare upload file metadata","type": "main","index": 0}]]},"Split message into emoji array": {"main": [[{"node": "Check if name exists","type": "main","index": 1}]]},"Fetch existing emojis (simulated)": {"main": [[{"node": "Check if name exists","type": "main","index": 0}]]}},"active": false,"settings": {"executionOrder": "v1"},"versionId": "f49da4e9-9d58-4548-a7bd-035d001c33bc","meta": {"templateCredsSetupCompleted": true,"instanceId": "96c6d670f4eb464180466e31ac97e81f48bd0fafe0fea50699c0a5c4f2d47f7d"},"id": "WObBmULfG6MRmKeP","tags": []}

内容的提问来源于stack exchange,提问作者Afshin Davoudy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 09:27:01