Basic Concepts: Referencing a node that was executed earlier in the workflow

Hi,
First of all, thanks for the awesome support and the rich features of n8n… I have a question which I believe is very basic one but for some reason I can’t figure out how get around this issue.
I fetch data from an API (Woocommerce) which I rearrange based on my needs. The function node generating the formatted data extracted from the API is Function-ProductItems. I have some function few nodes from the Function-ProductItems which does extraction of each Product/Item attributes. When I try to reference Function-ProductItems node, I get Error: items is not iterable . I tried different methods using json conversion methods such as tostring and JSON.stringify () but with no results. I am sure I am missing some basic concepts and would really appreciate your guidance.

Code for: Function-ProductVariations

const results = []
// *
const items = $node["Function-ProductItems"].json;
for (const item of items) {

  // **
  for (const variation of item.variations) {
    // ***
    for (const attribute of variation.variation_att) {
    results.push({ json: attribute })
    }
    // ***
  }
  // **
  
}
// *

return results;

Data fed by: Function-ProductItems

[
	{
		"item_title": "Fun Diving",
		"item_price": "35",
		"item_sku": "CO-ADV-03",
		"item_type": "variable",
		"item_cat": [
			"Adventures"
		],
		"variations": [
			{
				"variant_of": "CO-ADV-03",
				"item_group": [
					"Adventures"
				],
				"variation_id": 680,
				"variation_title": "Fun Diving Excluded",
				"variation_price": "35",
				"variation_sku": "CO-ADV-03-01",
				"variation_attributes": [
					{
						"variant_of": "CO-ADV-03",
						"attribute": "Diving Gear",
						"attribute_value": "Excluded",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Diving Gear",
						"item_attribute_values": [
							{
								"attribute_value": "Excluded",
								"abbr": "Excluded"
							}
						]
					}
				]
			},
			{
				"variant_of": "CO-ADV-03",
				"item_group": [
					"Adventures"
				],
				"variation_id": 681,
				"variation_title": "Fun Diving Included",
				"variation_price": "50",
				"variation_sku": "CO-ADV-03-02",
				"variation_attributes": [
					{
						"variant_of": "CO-ADV-03",
						"attribute": "Diving Gear",
						"attribute_value": "Included",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Diving Gear",
						"item_attribute_values": [
							{
								"attribute_value": "Included",
								"abbr": "Included"
							}
						]
					}
				]
			}
		]
	},
	{
		"item_title": "Kayaking",
		"item_price": "10",
		"item_sku": "CO-ACT-03",
		"item_type": "variable",
		"item_cat": [
			"Activities"
		],
		"variations": [
			{
				"variant_of": "CO-ACT-03",
				"item_group": [
					"Activities"
				],
				"variation_id": 735,
				"variation_title": "Kayaking single-kayak 1-hour",
				"variation_price": "10",
				"variation_sku": "CO-ACT-03-11",
				"variation_attributes": [
					{
						"variant_of": "CO-ACT-03",
						"attribute": "Kayak Type",
						"attribute_value": "single-kayak",
						"numeric_values": 0
					},
					{
						"variant_of": "CO-ACT-03",
						"attribute": "Duration",
						"attribute_value": "1-hour",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Kayak Type",
						"item_attribute_values": [
							{
								"attribute_value": "single-kayak",
								"abbr": "single-kayak"
							}
						]
					},
					{
						"attribute_name": "Duration",
						"item_attribute_values": [
							{
								"attribute_value": "1-hour",
								"abbr": "1-hour"
							}
						]
					}
				]
			},
			{
				"variant_of": "CO-ACT-03",
				"item_group": [
					"Activities"
				],
				"variation_id": 757,
				"variation_title": "Kayaking single-kayak 2-hours",
				"variation_price": "20",
				"variation_sku": "CO-ACT-03-12",
				"variation_attributes": [
					{
						"variant_of": "CO-ACT-03",
						"attribute": "Kayak Type",
						"attribute_value": "single-kayak",
						"numeric_values": 0
					},
					{
						"variant_of": "CO-ACT-03",
						"attribute": "Duration",
						"attribute_value": "2-hours",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Kayak Type",
						"item_attribute_values": [
							{
								"attribute_value": "single-kayak",
								"abbr": "single-kayak"
							}
						]
					},
					{
						"attribute_name": "Duration",
						"item_attribute_values": [
							{
								"attribute_value": "2-hours",
								"abbr": "2-hours"
							}
						]
					}
				]
			}
		]
	},
	{
		"item_title": "Banana Boat",
		"item_price": "35",
		"item_sku": "CO-ACT-02",
		"item_type": "variable",
		"item_cat": [
			"Activities"
		],
		"variations": [
			{
				"variant_of": "CO-ACT-02",
				"item_group": [
					"Activities"
				],
				"variation_id": 816,
				"variation_title": "Banana Boat 1-hour",
				"variation_price": "35",
				"variation_sku": "CO-ACT-02-01",
				"variation_attributes": [
					{
						"variant_of": "CO-ACT-02",
						"attribute": "Duration",
						"attribute_value": "1-hour",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Duration",
						"item_attribute_values": [
							{
								"attribute_value": "1-hour",
								"abbr": "1-hour"
							}
						]
					}
				]
			},
			{
				"variant_of": "CO-ACT-02",
				"item_group": [
					"Activities"
				],
				"variation_id": 817,
				"variation_title": "Banana Boat 2-hours",
				"variation_price": "70",
				"variation_sku": "CO-ACT-02-02",
				"variation_attributes": [
					{
						"variant_of": "CO-ACT-02",
						"attribute": "Duration",
						"attribute_value": "2-hours",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Duration",
						"item_attribute_values": [
							{
								"attribute_value": "2-hours",
								"abbr": "2-hours"
							}
						]
					}
				]
			}
		]
	},
	{
		"item_title": "Jet Skiing",
		"item_price": "25",
		"item_sku": "CO-ACT-01",
		"item_type": "variable",
		"item_cat": [
			"Activities"
		],
		"variations": [
			{
				"variant_of": "CO-ACT-01",
				"item_group": [
					"Activities"
				],
				"variation_id": 767,
				"variation_title": "Jet Skiing 1-hour",
				"variation_price": "25",
				"variation_sku": "CO-ACT-01-01",
				"variation_attributes": [
					{
						"variant_of": "CO-ACT-01",
						"attribute": "Duration",
						"attribute_value": "1-hour",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Duration",
						"item_attribute_values": [
							{
								"attribute_value": "1-hour",
								"abbr": "1-hour"
							}
						]
					}
				]
			},
			{
				"variant_of": "CO-ACT-01",
				"item_group": [
					"Activities"
				],
				"variation_id": 768,
				"variation_title": "Jet Skiing 2-hours",
				"variation_price": "50",
				"variation_sku": "CO-ACT-01-02",
				"variation_attributes": [
					{
						"variant_of": "CO-ACT-01",
						"attribute": "Duration",
						"attribute_value": "2-hours",
						"numeric_values": 0
					}
				],
				"variation_att": [
					{
						"attribute_name": "Duration",
						"item_attribute_values": [
							{
								"attribute_value": "2-hours",
								"abbr": "2-hours"
							}
						]
					}
				]
			}
		]
	}
]

It sounds like what you’re looking for is the $items() method instead of $node. When running console.log($node["Function-ProductItems"].json) you can see $node refers to a single item, so can’t be iterated in the way you describe.

So if you modify your function code just slightly (replacing $node with $items and item.variations with item.json.variations as per n8n’s data structure) this should work:

const results = []
// *
const items = $items("Function-ProductItems");
for (const item of items) {

  // **
  for (const variation of item.json.variations) {
    // ***
    for (const attribute of variation.variation_att) {
    results.push({ json: attribute })
    }
    // ***
  }
  // **
  
}
// *

return results;

Example Workflow

Hope this helps :slight_smile:

@MutedJam Thanksss, I knew it’s something I have missed. Appreciate your help…

You are more than welcome! If you have no follow-up questions on this it would be great if you could mark the topic as solved. Thanks so much!