Skip to content

Commit

Permalink
fix paired items handling in the merge node
Browse files Browse the repository at this point in the history
This happens only when choosing a branch and selecting to return an empty object.

I stumbled on this while attempting to solve the level 2 course.
  • Loading branch information
despairblue committed Jan 29, 2024
1 parent 49062d1 commit 26c00b1
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "8d731d98fab18fb8e68289d44c50faa5d5c5378aedc3f892abdd7d5d3b7061c5"
},
"nodes": [
{
"parameters": {
"fields": {
"values": [
{
"name": "one",
"stringValue": "={{ $('Code').item.json.id }}"
},
{
"name": "two",
"stringValue": "={{ $('Code1').item.json.id }}"
}
]
},
"options": {}
},
"id": "853f9e11-e59b-4f21-81ba-08ec2d69c87a",
"name": "Edit Fields",
"type": "n8n-nodes-base.set",
"typeVersion": 3.2,
"position": [
900,
580
]
},
{
"parameters": {},
"id": "8310a9da-b02b-4a77-80d4-7c2de5bcbae8",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
180,
600
]
},
{
"parameters": {
"jsCode": "return [\n {id: 1},\n];"
},
"id": "0f1fb40d-0c6c-455d-bb3b-0c0fa818e063",
"name": "Code",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [
420,
520
]
},
{
"parameters": {
"jsCode": "return [\n {id: 2},\n];"
},
"id": "873fcccc-86c1-40fd-bdae-2bb31e971382",
"name": "Code1",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [
420,
680
]
},
{
"parameters": {
"mode": "chooseBranch",
"output": "empty"
},
"id": "b81a2372-9f8b-4899-b2fd-c581bcf930d8",
"name": "Merge3",
"type": "n8n-nodes-base.merge",
"typeVersion": 2,
"position": [
700,
580
]
}
],
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
},
{
"node": "Code1",
"type": "main",
"index": 0
}
]
]
},
"Code": {
"main": [
[
{
"node": "Merge3",
"type": "main",
"index": 0
}
]
]
},
"Code1": {
"main": [
[
{
"node": "Merge3",
"type": "main",
"index": 1
}
]
]
},
"Merge3": {
"main": [
[
{
"node": "Edit Fields",
"type": "main",
"index": 0
}
]
]
}
},
"pinData": {
"Edit Fields": [
{
"json": {
"one": "1",
"two": "2"
}
}
]
}
}
33 changes: 30 additions & 3 deletions packages/nodes-base/nodes/Merge/v2/MergeV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
} from './GenericFunctions';

import { optionsDescription } from './OptionsDescription';
import { generatePairedItemData } from '../../../utils/utilities';

const versionDescription: INodeTypeDescription = {
displayName: 'Merge',
Expand Down Expand Up @@ -605,8 +604,36 @@ export class MergeV2 implements INodeType {
returnData.push.apply(returnData, this.getInputData(1));
}
if (output === 'empty') {
const itemData = generatePairedItemData(this.getInputData(0).length);
returnData.push({ json: {}, pairedItem: itemData });
// TODO: was necessary to satisfies types.
// I'm wondering if there is already a function that does exactly that? That turns
// Array<IPairedItemData | IPairedItemData[] | number | undefined> -> IPairedItemData[]
// or
// IPairedItemData | IPairedItemData[] | number | undefined -> IPairedItemData | undefined
function numberToPairedItem(
possiblyPairedItems: Array<INodeExecutionData['pairedItem']>,
): IPairedItemData[] {
return possiblyPairedItems.reduce((pairedItems: IPairedItemData[], pairedItem) => {
if (typeof pairedItem === 'number') {
pairedItems.push({ item: pairedItem });
} else if (Array.isArray(pairedItem)) {
pairedItems.push(...pairedItem);
} else if (pairedItem) {
pairedItems.push(pairedItem);
}

return pairedItems;
}, [] as IPairedItemData[]);
}

const pairedItem = numberToPairedItem([
...this.getInputData(0).map((inputData) => inputData.pairedItem),
...this.getInputData(1).map((inputData) => inputData.pairedItem),
]);

returnData.push({
json: {},
pairedItem,
});
}
}
}
Expand Down

0 comments on commit 26c00b1

Please sign in to comment.