Code node always throw an error about returned value

Describe the issue/error/question

Hello ! I am willing to do a simple calculation in code node to sum some values of an input array as below:


[
  {
  "id": "11422469276",
  "properties": {
    "amount": "69",
}
},
{
  "id": "11410323382",
  "properties": {
    "amount": "167",
  },
}
]

I am willing to sum the amounts of all objects (1,000 approx.)

I am doing this with this simple calculation (where ($(‘HubSpot’).all())) is the object above:

const calculate = (hubspot) => {
let totalAmount = 0;
console.log(hubspot)
hubspot.forEach((obj) => {
  totalAmount += Number(obj.json.properties.amount);
  console.log(totalAmount.toFixed(0))
});

const object = {"total": totalAmount}
return object;
}

calculate($('HubSpot').all());

I made this because the error message forces us to return an object.
I tried a lot of different return values and none of them are working.

Could you help me fixing this ?

Thank you for your help, and your patience, i am new to this

Welcome to the community @Nicolas_AUBRUN!

The problem here is that you actually do not return anything at all, which is then like returning undefined, which is not a valid response.

Now assuming your code is correct (did not have a look at it at all) you could simply change the last line to:

return [
  {
    result: calculate($('HubSpot').all())
  }
]
1 Like

Thank you very much for your help, I didn’t thought about it in this way, it’s working well now, thank you !!

Nicolas

1 Like

Ah great to hear that your problem could be solved easily.

Have fun!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.