fix(Item Lists Node): Concatenate operation pairedItems fix (#7286)

Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
This commit is contained in:
Michael Kret 2023-09-28 10:20:54 +03:00 committed by GitHub
parent 0132514f8b
commit cde23a1bb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 164 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import type {
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
IPairedItemData,
} from 'n8n-workflow';
import { NodeOperationError, deepCopy } from 'n8n-workflow';
@ -1103,6 +1104,7 @@ return 0;`,
return [returnData];
} else {
let newItems: IDataObject[] = items.map((item) => item.json);
let pairedItem: IPairedItemData[] = [];
const destinationFieldName = this.getNodeParameter('destinationFieldName', 0) as string;
const fieldsToExclude = (
this.getNodeParameter('fieldsToExclude.fields', 0, []) as IDataObject[]
@ -1112,7 +1114,7 @@ return 0;`,
).map((entry) => entry.fieldName);
if (fieldsToExclude.length || fieldsToInclude.length) {
newItems = newItems.reduce((acc, item) => {
newItems = newItems.reduce((acc, item, index) => {
const newItem: IDataObject = {};
let outputFields = Object.keys(item);
@ -1132,11 +1134,16 @@ return 0;`,
if (isEmpty(newItem)) {
return acc;
}
pairedItem.push({ item: index });
return acc.concat([newItem]);
}, [] as IDataObject[]);
} else {
pairedItem = Array.from({ length: newItems.length }, (_, item) => ({
item,
}));
}
return [[{ json: { [destinationFieldName]: newItems } }]];
return [[{ json: { [destinationFieldName]: newItems }, pairedItem }]];
}
} else if (operation === 'removeDuplicates') {
const compare = this.getNodeParameter('compare', 0) as string;

View file

@ -3,6 +3,7 @@ import type {
IExecuteFunctions,
INodeExecutionData,
INodeProperties,
IPairedItemData,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
@ -307,6 +308,7 @@ export async function execute(
returnData.push(newItem);
} else {
let newItems: IDataObject[] = items.map((item) => item.json);
let pairedItem: IPairedItemData[] = [];
const destinationFieldName = this.getNodeParameter('destinationFieldName', 0) as string;
const fieldsToExclude = prepareFieldsArray(
@ -320,7 +322,7 @@ export async function execute(
);
if (fieldsToExclude.length || fieldsToInclude.length) {
newItems = newItems.reduce((acc, item) => {
newItems = newItems.reduce((acc, item, index) => {
const newItem: IDataObject = {};
let outputFields = Object.keys(item);
@ -340,12 +342,17 @@ export async function execute(
if (isEmpty(newItem)) {
return acc;
}
pairedItem.push({ item: index });
return acc.concat([newItem]);
}, [] as IDataObject[]);
} else {
pairedItem = Array.from({ length: newItems.length }, (_, item) => ({
item,
}));
}
const output: INodeExecutionData = { json: { [destinationFieldName]: newItems } };
const output: INodeExecutionData = { json: { [destinationFieldName]: newItems }, pairedItem };
returnData.push(output);
}

View file

@ -0,0 +1,145 @@
{
"name": "itemsList concatenate paired fix",
"nodes": [
{
"parameters": {},
"id": "37256a71-67fe-4643-b4c7-e670096b68fc",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
620,
540
]
},
{
"parameters": {
"category": "randomData",
"randomDataSeed": "n8n",
"randomDataCount": 3
},
"id": "0518c1d3-2e6d-40c6-8225-d89ec28ed28d",
"name": "DebugHelper",
"type": "n8n-nodes-base.debugHelper",
"typeVersion": 1,
"position": [
1000,
540
]
},
{
"parameters": {
"operation": "concatenateItems",
"aggregate": "aggregateAllItemData"
},
"id": "7866056b-a7c1-41e2-b3b7-301cb21d95d4",
"name": "Item Lists",
"type": "n8n-nodes-base.itemLists",
"typeVersion": 3,
"position": [
1200,
540
]
},
{
"parameters": {
"fields": {
"values": [
{
"name": "foo",
"stringValue": "bar"
}
]
},
"options": {}
},
"id": "191ec112-65b6-4e4a-bf11-b63ab7e96f68",
"name": "Edit Fields",
"type": "n8n-nodes-base.set",
"typeVersion": 3,
"position": [
800,
540
]
},
{
"parameters": {
"customerId": "1",
"message": "={{ $('Edit Fields').item.json.foo }}"
},
"id": "4a3d6844-5044-4570-912a-af3f32efa871",
"name": "Customer Messenger (n8n training)",
"type": "n8n-nodes-base.n8nTrainingCustomerMessenger",
"typeVersion": 1,
"position": [
1400,
540
]
}
],
"pinData": {
"Customer Messenger (n8n training)": [
{
"json": {
"output": "Sent message to customer 1: bar"
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Edit Fields",
"type": "main",
"index": 0
}
]
]
},
"DebugHelper": {
"main": [
[
{
"node": "Item Lists",
"type": "main",
"index": 0
}
]
]
},
"Item Lists": {
"main": [
[
{
"node": "Customer Messenger (n8n training)",
"type": "main",
"index": 0
}
]
]
},
"Edit Fields": {
"main": [
[
{
"node": "DebugHelper",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1"
},
"versionId": "d17719c9-c625-4419-8b3c-d4cfaeffc312",
"id": "vXdwDVBSRxZxMrcv",
"meta": {
"instanceId": "b888bd11cd1ddbb95450babf3e199556799d999b896f650de768b8370ee50363"
},
"tags": []
}