I fully understood FuntionItem node in the end from checking the source code, but documentation is confusing in several ways. That's because Function and FunctionItem have different styles. Function gives access to the whole items array and the structure is transparently available with json/binary. In case of FunctionItem. The item is not item, it's item.json. You can't access item.binary cause of this, as it would be intuitive to do so. At first an update to the documentation to guide new users. It should be also considered that Function and FunctionItem behave the same, e.g. item.json and item.binary should exists. The variable might also called itemjson instead of item.
1.7 KiB
Function and Function Item Nodes
These are the most powerful nodes in n8n. With these, almost everything can be done if you know how to write JavaScript code. Both nodes work very similarly. They give you access to the incoming data and you can manipulate it.
Difference between both nodes
The difference is that the code of the Function node gets executed only once. It receives the full items (JSON and binary data) as an array and expects an array of items as a return value. The items returned can be totally different from the incoming ones. So it is not only possible to remove and edit existing items, but also to add or return totally new ones.
The code of the Function Item node on the other hand gets executed once for every item. It receives
one item at a time as input and also just the JSON data. As a return value, it expects the JSON data
of one single item. That makes it possible to add, remove and edit JSON properties of items
but it is not possible to add new or remove existing items. Accessing and changing binary data is only
possible via the methods getBinaryData and setBinaryData.
Both nodes support promises. So instead of returning the item or items directly, it is also possible to return a promise which resolves accordingly.
Comparison
| Data to access ... | Function | FunctionItem |
|---|---|---|
| JSON-Data | items[index].json | item |
| Binary-Data | items[index].binary | getBinaryData() |