Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Always stringify data of Function-Nodes #2606

Merged
merged 2 commits into from Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/nodes-base/nodes/Function/Function.node.ts
@@ -1,5 +1,6 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
Expand Down Expand Up @@ -58,6 +59,21 @@ return items;`,
// Copy the items as they may get changed in the functions
items = JSON.parse(JSON.stringify(items));

const cleanupData = (inputData: IDataObject): IDataObject => {
Object.keys(inputData).map(key => {
if (inputData[key] !== null && typeof inputData[key] === 'object') {
if (inputData[key]!.constructor.name === 'Object') {
// Is regular node.js object so check its data
inputData[key] = cleanupData(inputData[key] as IDataObject);
} else {
// Is some special object like a Date so stringify
inputData[key] = JSON.parse(JSON.stringify(inputData[key]));
}
}
});
return inputData;
};

// Define the global objects for the custom function
const sandbox = {
getNodeParameter: this.getNodeParameter,
Expand Down Expand Up @@ -117,6 +133,9 @@ return items;`,
if (typeof item.json !== 'object') {
throw new NodeOperationError(this.getNode(), 'The json-property has to be an object!');
}

item.json = cleanupData(item.json);

if (item.binary !== undefined) {
if (Array.isArray(item.binary) || typeof item.binary !== 'object') {
throw new NodeOperationError(this.getNode(), 'The binary-property has to be an object!');
Expand All @@ -143,9 +162,6 @@ return items;`,
}
}




return this.prepareOutputData(items);
}
}
17 changes: 16 additions & 1 deletion packages/nodes-base/nodes/FunctionItem/FunctionItem.node.ts
Expand Up @@ -58,6 +58,21 @@ return item;`,
const length = items.length as unknown as number;
let item: INodeExecutionData;

const cleanupData = (inputData: IDataObject): IDataObject => {
Object.keys(inputData).map(key => {
if (inputData[key] !== null && typeof inputData[key] === 'object') {
if (inputData[key]!.constructor.name === 'Object') {
// Is regular node.js object so check its data
inputData[key] = cleanupData(inputData[key] as IDataObject);
} else {
// Is some special object like a Date so stringify
inputData[key] = JSON.parse(JSON.stringify(inputData[key]));
}
}
});
return inputData;
};

for (let itemIndex = 0; itemIndex < length; itemIndex++) {
try {
item = items[itemIndex];
Expand Down Expand Up @@ -145,7 +160,7 @@ return item;`,
}

const returnItem: INodeExecutionData = {
json: jsonData,
json: cleanupData(jsonData),
};

if (item.binary) {
Expand Down