Function and FunctionItem difference

Hey there!

Apart from the code and the name ist it possible you can clarify the difference between Function and Funciton Item nodes?

Regards Sebastian

Ah yes that is actually quite simple.

The code of the Function-Node gets executed only once. It gets as input all the items and returns all the items (which can be totally different ones). So with that node, it is possible to add, remove, changed, replace items. That is why you have to return an array of items in the code.

The Function Item-Node does get executed once for each item. So if you load all the deals from Pipedrive and it returns 100 it will execute that code 100x. One time for each item. There it is only possible to edit an item like adding or updating a property. That is why you have to return a single item in the code.

Hope that makes sense.

i guess the description (0.29.0 here) should be edited:

image

Uh yes, you are right. Thanks for informing me about that. Guess I messed that up. Sorry! Fixed it:

Will be released with the next version.

Thanks for the explanation.

I tried to use Functionitem, but I was not able to reproduce the behaviour.
previous node has an array of items:

I connect it with a FunctionItem.

I expected to show now an array with the same object.

If I use this code, it just forwards the item - array 1-to-1

Thanks in advance for any tips.

Sorry but I do not understand the problem. It seems to be working perfectly.

The code in the node defines what to return. In your first example, you hardcode the value to return. Which is “test: 1”. So it ignores everything that entered the node and returns what you told it to.

In the second example, you tell it to return exactly what entered the node. Which is the array which is currently saved as first item. And so it returns that array.

If you want to add “test: 1” to for example the first array item you would do:

item[0].test  = 1;
return item;

What do you want to do exactly? Maybe I can then help you to do it.

Hi Jan

aha, 1st item, refers to 1st time a node is executed.
It does not refer to the 1st item of the array in the message.

Is that correct?

Ah no. The execution has nothing to do with it. It is really the first item.

But I think I know now what is confusing you. It is that your only item is an array.

If you look in your screenshots you can see that it says “Results: 1”. So there is only one item, not multiple ones. Normally is an item a JSON-Object like this: { test: 1 } and not an array of objects like in your case.

So to work like you expect it to work you have to split all the array-elements which are currently in the first item into separate items. That can be done like this with a Function-Node.

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return items[0].json.map((item) => {\n  return {\n    json: item\n  };\n});"
      },
      "name": "Split Array in Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        727,
        484
      ]
    }
  ],
  "connections": {}
}

Thanks so much! that was it.

Hoping to be able to contribute back to the community soon.

Nothing to thank for! It is probably more for me to say sorry as I did not find the time yet to create proper documentation for many parts of n8n. Sadly did not get to it yet as always a lot of other stuff is coming up. But promise it will improve in the future!

Still, hope you enjoy working with n8n! Great to have you as part of the community!