Greetings!
I’m in the process of making a node that takes a .json-file from another node and calculates the average of the included numbers.
async execute (this: IExecuteFunctions): Promise<INodeExecutionData[][]>{
const array = this.getInputData();
const arraySize = array.length as number;
let average : number;
let sum : number = 0;
for (let i = 0; i< arraySize; i++){
sum += this.getNodeParameter('value', i) as number;
console.log(array[i]+" "+ sum);
};
average = sum/arraySize;
let resultMessage: IDataObject = { "value": average };
let resultItem: INodeExecutionData = { json: resultMessage };
return [[resultItem]];
}
If I run the node in a workflow however the function doesn’t seem to get the value of the json array.
I’m pretty sure I just don’t get how getNodeParameter works.
Could you please give me some input?
Hi @Maxleton, first of all welcome to the community!
getNodeParameter would return to the value of a node parameter but from your description I understand you want to access the input items, is that correct?
If so, you could base your logic on your array constant rather than using getNodeParameter. E.g. like so:
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const array = this.getInputData();
const arraySize = array.length as number;
let average : number;
let sum : number = 0;
for (let i = 0; i< arraySize; i++){
sum += array[i].json.value as number;
console.log(array[i]+" "+ sum);
};
average = sum/arraySize;
let resultMessage: IDataObject = { "value": average };
let resultItem: INodeExecutionData = { json: resultMessage };
return [[resultItem]];
}
Hi @MutedJam
I am trying to use this code to create an average for the column “rating” and again for the “calc_sentiment” column, but struggling as this level of coding is way about my pay grade!
Is there any chance you can help?
Thanks - Jack