From 54e3838daed1f0931a04ba76cfd1ea7519c0e382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Tue, 30 May 2023 10:56:55 +0000 Subject: [PATCH] fix(Code Node): Fix `item` and `items` alias regression (#6331) --- packages/nodes-base/nodes/Code/Code.node.ts | 7 +- .../nodes/Code/test/Code.workflow.json | 93 ++++++++++++------- 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/packages/nodes-base/nodes/Code/Code.node.ts b/packages/nodes-base/nodes/Code/Code.node.ts index 0cd52d3af8..3af95ccccb 100644 --- a/packages/nodes-base/nodes/Code/Code.node.ts +++ b/packages/nodes-base/nodes/Code/Code.node.ts @@ -102,13 +102,18 @@ export class Code implements INodeType { const getSandbox = (index = 0) => { const code = this.getNodeParameter(codeParameterName, index) as string; const context = getSandboxContext.call(this, index); + if (nodeMode === 'runOnceForAllItems') { + context.items = context.$input.all(); + } else { + context.item = context.$input.item; + } + if (language === 'python') { const modules = this.getNodeParameter('modules', index) as string; const moduleImports: string[] = modules ? modules.split(',').map((m) => m.trim()) : []; context.printOverwrite = workflowMode === 'manual' ? this.sendMessageToUI : null; return new PythonSandbox(context, code, moduleImports, index, this.helpers); } else { - context.items = context.$input.all(); const sandbox = new JavaScriptSandbox(context, code, index, workflowMode, this.helpers); if (workflowMode === 'manual') { sandbox.vm.on('console.log', this.sendMessageToUI); diff --git a/packages/nodes-base/nodes/Code/test/Code.workflow.json b/packages/nodes-base/nodes/Code/test/Code.workflow.json index 02bef9a164..07933b0f82 100644 --- a/packages/nodes-base/nodes/Code/test/Code.workflow.json +++ b/packages/nodes-base/nodes/Code/test/Code.workflow.json @@ -1,56 +1,64 @@ { - "name": "My workflow 24", "nodes": [ { "parameters": {}, - "id": "3691826c-caf3-4773-b0af-f5fe2eda42bc", + "id": "33eede8d-2ab0-42ab-b79a-a069d8549ab0", "name": "When clicking \"Execute Workflow\"", "type": "n8n-nodes-base.manualTrigger", "typeVersion": 1, - "position": [ - -960, - 600 - ] + "position": [-40, 580] }, { "parameters": { "jsCode": "return[\n { value: 1 },\n { value: 2 },\n]" }, - "id": "149e8223-20e2-480e-b675-2aeb6a9f9095", + "id": "a5913b52-24dc-4f81-bb7f-f90e61dad978", "name": "Sample Data", "type": "n8n-nodes-base.code", "typeVersion": 1, - "position": [ - -720, - 600 - ] + "position": [200, 580] }, { "parameters": { "jsCode": "// Loop over input items and add a new field\n// called 'myNewField' to the JSON of each one\nlet sum = 0;\nfor (const item of $input.all()) {\n sum += item.json.value;\n}\n\nreturn [ {sum} ];" }, - "id": "2002ec71-fd88-4a5b-b2fe-0071fc5397e4", + "id": "c4ad4913-5af3-42bc-a784-69182f1facdd", "name": "Run Once for All Items", "type": "n8n-nodes-base.code", "typeVersion": 1, - "position": [ - -460, - 480 - ] + "position": [460, 320] + }, + { + "parameters": { + "jsCode": "// Loop over input items and add a new field\n// called 'myNewField' to the JSON of each one\nlet sum = 0;\nfor (const item of items) {\n sum += item.json.value;\n}\n\nreturn [ {sum} ];" + }, + "id": "34cbd204-4335-4790-92cd-c3df617eee21", + "name": "Run Once for All Items (Legacy Syntax)", + "type": "n8n-nodes-base.code", + "typeVersion": 1, + "position": [460, 500] }, { "parameters": { "mode": "runOnceForEachItem", "jsCode": "// Add a new field called 'myNewField' to the\n// JSON of the item\n$input.item.json.myNewField = $input.item.json.value;\n\nreturn $input.item;" }, - "id": "9adbeb7a-c711-4ff6-881e-96d5e122c2bc", + "id": "f67d29bf-554a-4572-8867-4456182dec24", "name": "Run Once for Each Item", "type": "n8n-nodes-base.code", "typeVersion": 1, - "position": [ - -460, - 720 - ] + "position": [460, 680] + }, + { + "parameters": { + "mode": "runOnceForEachItem", + "jsCode": "// Add a new field called 'myNewField' to the\n// JSON of the item\nitem.json.myNewField = item.json.value;\n\nreturn item;" + }, + "id": "6f4bf149-e84e-4e0d-802a-7eaf7a42b18c", + "name": "Run Once for Each Item (Legacy Syntax)", + "type": "n8n-nodes-base.code", + "typeVersion": 1, + "position": [460, 860] } ], "pinData": { @@ -74,6 +82,27 @@ "myNewField": 2 } } + ], + "Run Once for All Items (Legacy Syntax)": [ + { + "json": { + "sum": 3 + } + } + ], + "Run Once for Each Item (Legacy Syntax)": [ + { + "json": { + "value": 1, + "myNewField": 1 + } + }, + { + "json": { + "value": 2, + "myNewField": 2 + } + } ] }, "connections": { @@ -100,17 +129,19 @@ "node": "Run Once for Each Item", "type": "main", "index": 0 + }, + { + "node": "Run Once for All Items (Legacy Syntax)", + "type": "main", + "index": 0 + }, + { + "node": "Run Once for Each Item (Legacy Syntax)", + "type": "main", + "index": 0 } ] ] } - }, - "active": false, - "settings": {}, - "versionId": "b0d8ec77-92ab-4fa7-93b1-8a2e3543059d", - "id": "181", - "meta": { - "instanceId": "104a4d08d8897b8bdeb38aaca515021075e0bd8544c983c2bb8c86e6a8e6081c" - }, - "tags": [] -} \ No newline at end of file + } +}