Item linking in the Code node#
Use n8n's item linking to access data from items that precede the current item. It also has implications when using the Code node. Most nodes link every output item to an input item. This creates a chain of items that you can work back along to access previous items. For a deeper conceptual overview of this topic, refer to Item linking concepts. This document focuses on practical usage examples.
When using the Code node, there are some scenarios where you need to manually supply item linking information if you want to be able to use $("<node-name>").item
later in the workflow. All these scenarios only apply if you have more than one incoming item. n8n automatically handles item linking for single items.
These scenarios are when you:
- Add new items: the new items aren't linked to any input.
- Return new items.
- Want to manually control the item linking.
n8n's automatic item linking handles the other scenarios.
To control item linking, set pairedItem
when returning data. For example, to link to the item at index 0:
1 2 3 4 5 6 7 8 9 |
|
pairedItem
usage example#
Take this input data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
And use it to generate new items, containing just the name, along with a new piece of data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
newItems
is an array of items with no pairedItem
. This means there's no way to trace back from these items to the items used to generate them.
Add the pairedItem
object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Each new item now links to the item used to create it.