Getting value via getNodeParameter

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]];  
    }

The input looks like this:

[{    json: {     "value": '2'    }  },    {    json: {      "value": '3'    }  }]

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]];
}

For an input like so:

The above code would return a single item looking like this:

Is this what you had in mind?

1 Like

Yes!
Thank you, that is exactly what I’ve been looking for :]

1 Like