fix(Code Node): Fix item and items alias regression (#6331)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-05-30 10:56:55 +00:00 committed by GitHub
parent 77e3f1551d
commit 54e3838dae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 32 deletions

View file

@ -102,13 +102,18 @@ export class Code implements INodeType {
const getSandbox = (index = 0) => { const getSandbox = (index = 0) => {
const code = this.getNodeParameter(codeParameterName, index) as string; const code = this.getNodeParameter(codeParameterName, index) as string;
const context = getSandboxContext.call(this, index); const context = getSandboxContext.call(this, index);
if (nodeMode === 'runOnceForAllItems') {
context.items = context.$input.all();
} else {
context.item = context.$input.item;
}
if (language === 'python') { if (language === 'python') {
const modules = this.getNodeParameter('modules', index) as string; const modules = this.getNodeParameter('modules', index) as string;
const moduleImports: string[] = modules ? modules.split(',').map((m) => m.trim()) : []; const moduleImports: string[] = modules ? modules.split(',').map((m) => m.trim()) : [];
context.printOverwrite = workflowMode === 'manual' ? this.sendMessageToUI : null; context.printOverwrite = workflowMode === 'manual' ? this.sendMessageToUI : null;
return new PythonSandbox(context, code, moduleImports, index, this.helpers); return new PythonSandbox(context, code, moduleImports, index, this.helpers);
} else { } else {
context.items = context.$input.all();
const sandbox = new JavaScriptSandbox(context, code, index, workflowMode, this.helpers); const sandbox = new JavaScriptSandbox(context, code, index, workflowMode, this.helpers);
if (workflowMode === 'manual') { if (workflowMode === 'manual') {
sandbox.vm.on('console.log', this.sendMessageToUI); sandbox.vm.on('console.log', this.sendMessageToUI);

View file

@ -1,56 +1,64 @@
{ {
"name": "My workflow 24",
"nodes": [ "nodes": [
{ {
"parameters": {}, "parameters": {},
"id": "3691826c-caf3-4773-b0af-f5fe2eda42bc", "id": "33eede8d-2ab0-42ab-b79a-a069d8549ab0",
"name": "When clicking \"Execute Workflow\"", "name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger", "type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1, "typeVersion": 1,
"position": [ "position": [-40, 580]
-960,
600
]
}, },
{ {
"parameters": { "parameters": {
"jsCode": "return[\n { value: 1 },\n { value: 2 },\n]" "jsCode": "return[\n { value: 1 },\n { value: 2 },\n]"
}, },
"id": "149e8223-20e2-480e-b675-2aeb6a9f9095", "id": "a5913b52-24dc-4f81-bb7f-f90e61dad978",
"name": "Sample Data", "name": "Sample Data",
"type": "n8n-nodes-base.code", "type": "n8n-nodes-base.code",
"typeVersion": 1, "typeVersion": 1,
"position": [ "position": [200, 580]
-720,
600
]
}, },
{ {
"parameters": { "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} ];" "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", "name": "Run Once for All Items",
"type": "n8n-nodes-base.code", "type": "n8n-nodes-base.code",
"typeVersion": 1, "typeVersion": 1,
"position": [ "position": [460, 320]
-460, },
480 {
] "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": { "parameters": {
"mode": "runOnceForEachItem", "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;" "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", "name": "Run Once for Each Item",
"type": "n8n-nodes-base.code", "type": "n8n-nodes-base.code",
"typeVersion": 1, "typeVersion": 1,
"position": [ "position": [460, 680]
-460, },
720 {
] "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": { "pinData": {
@ -74,6 +82,27 @@
"myNewField": 2 "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": { "connections": {
@ -100,17 +129,19 @@
"node": "Run Once for Each Item", "node": "Run Once for Each Item",
"type": "main", "type": "main",
"index": 0 "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": []
} }