IF node FALSE branch only outputs last iteration — how to get all loop data combined?

Describe the problem/error/question

I have a pagination loop built using HTTP Request → Code Node → IF Node → loop back to HTTP Request. The loop is working correctly — it fetches all pages using a pagination[last_id] cursor and loops back until there are no more pages.

The problem: When the loop finishes and exits via the FALSE branch of the IF node, only the last iteration’s data is passed to the next node. I need ALL the data from EVERY iteration combined into a single output.

I tried:

  • Accumulating data inside the Code node using $('NodeName').all() — does not return previous loop iterations

  • Using an Aggregate node after the IF node’s FALSE branch — only receives 1 item (the last loop output)

  • Storing accumulated data in a Set node and reading it back — the Set node value does not persist/update across loop iterations

  • Passing the full accumulated array through each loop iteration via the output JSON — the FALSE branch still only outputs the last iteration’s single item

The core issue: N8N’s IF node FALSE branch only outputs ONE item when a loop exits. There is no way to collect or aggregate data across multiple loop iterations into a single combined output.

Question: How do I properly accumulate and combine data from multiple iterations of a loop (HTTP → Code → IF → loop back) so that when the loop exits, I have ALL the data combined in one output?


What is the error message (if any)?

No error. The workflow runs successfully, but the FALSE branch of the IF node only passes through the data from the very last loop iteration, discarding all previously fetched pages.


Please share your workflow


Share the output returned by the last node

Output from “Accumulate Data” on each loop iteration (console logs):

Page 1: Retrieved 100 POs. Total: 100
  ➡️  Next cursor: 1109
---
Page 2: Retrieved 100 POs. Total: 100
  ➡️  Next cursor: 1209
---
Page 3: Retrieved 100 POs. Total: 100
  ➡️  Next cursor: 1309
...
Page 11: Retrieved 9 POs. Total: 9
  🏁 No more pages

Output received on the FALSE branch (after loop exits):

{
  "last_id": null,
  "all_purchase_orders": [/* only 9 items - last page only */],
  "page_count": 11,
  "has_more": false
}

Expected output on the FALSE branch:

{
  "last_id": null,
  "all_purchase_orders": [/* ALL 1109 items combined from all 11 pages */],
  "page_count": 11,
  "has_more": false
}


Information on your n8n setup

  • n8n version: latest (cloud)

  • Database (default: SQLite): Cloud managed

  • n8n EXECUTIONS_PROCESS setting (default: own, main): default

  • Running n8n via: n8n cloud

  • Operating system: N/A (cloud)

Hi @rohanurrahman, I think this issue is that the node handles each execution per itineration. The loop exits via the false branch and only looks at tht state of the last item.

My option would be to place an Agrregate Items node after the loop and connect it to the HTTP Request node. In this case the false branch ot the IF node triggers the Aggregate. This way it reaches back and collects every item.

Maybe you can try this code?

{
“nodes”: [
{
“parameters”: {},
“type”: “n8n-nodes-base.manualTrigger”,
“typeVersion”: 1,
“position”: [
-2880,
-16
],
“id”: “eb50cb3f-1581-4943-8298-6afe029917fa”,
“name”: “When clicking ‘Execute workflow’”
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “page_count”,
“name”: “page_count”,
“value”: “=0”,
“type”: “number”
}
]
},
“options”: {}
},
“id”: “fe079775-7122-40ec-9847-8992b5c8a963”,
“name”: “Initialize Variables”,
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.3,
“position”: [
-2384,
-40
]
},
{
“parameters”: {
“url”: “=https://jackandsage.app.apparelmagic.com/api/json/purchase_orders/?token=878d3115d038b50628e2acbeae4e5c02&time={{ Math.floor(Date.now() / 1000) }}{{ $json.last_id ? ‘&pagination[last_id]=’ + $json.last_id : ‘’ }}”,
“sendHeaders”: true,
“headerParameters”: {
“parameters”: [
{
“name”: “Accept”,
“value”: “application/json”
}
]
},
“options”: {
“response”: {
“response”: {
“responseFormat”: “json”
}
},
“timeout”: 60000
}
},
“id”: “7a6acff4-c390-496a-8e31-3f6c73437af0”,
“name”: “HTTP - Fetch Page”,
“type”: “n8n-nodes-base.httpRequest”,
“typeVersion”: 4.2,
“position”: [
-2160,
-40
]
},
{
“parameters”: {
“jsCode”: “const currentResponse = $input.item.json;\nconst nextLastId = currentResponse.meta?.pagination?.last_id || null;\nconst pageCount = ($node["HTTP - Fetch Page"].runIndex || 0) + 1;\n\nreturn {\n json: {\n last_id: nextLastId,\n page_count: pageCount,\n has_more: !!nextLastId\n }\n};”
},
“id”: “1f53015e-7005-47b4-a2c5-57119204fe42”,
“name”: “Check Pagination”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
-1936,
-112
]
},
{
“parameters”: {
“aggregate”: “aggregateAllItemData”,
“destinationFieldName”: “all_purchase_orders”
},
“id”: “agg-items-001”,
“name”: “Aggregate All Data”,
“type”: “n8n-nodes-base.aggregate”,
“typeVersion”: 1,
“position”: [
-1450,
150
]
},
{
“parameters”: {
“jsCode”: “await new Promise(resolve => setTimeout(resolve, 300));\nreturn $input.all();”
},
“id”: “911d4c29-febe-4436-a69f-fa6f0df7360d”,
“name”: “Delay”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
-1264,
-40
]
},
{
“parameters”: {
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 1
},
“conditions”: [
{
“id”: “has-more”,
“leftValue”: “={{ $json.has_more }}”,
“rightValue”: true,
“operator”: {
“type”: “boolean”,
“operation”: “equals”
}
}
],
“combinator”: “and”
},
“options”: {}
},
“id”: “703ae569-662e-409d-8166-9f9eb93fbd98”,
“name”: “Has More Pages?”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 2,
“position”: [
-1712,
-112
]
}
],
“connections”: {
“When clicking ‘Execute workflow’”: {
“main”: [
[
{
“node”: “Initialize Variables”,
“type”: “main”,
“index”: 0
}
]
]
},
“Initialize Variables”: {
“main”: [
[
{
“node”: “HTTP - Fetch Page”,
“type”: “main”,
“index”: 0
}
]
]
},
“HTTP - Fetch Page”: {
“main”: [
[
{
“node”: “Check Pagination”,
“type”: “main”,
“index”: 0
},
{
“node”: “Aggregate All Data”,
“type”: “main”,
“index”: 0
}
]
]
},
“Check Pagination”: {
“main”: [
[
{
“node”: “Has More Pages?”,
“type”: “main”,
“index”: 0
}
]
]
},
“Delay”: {
“main”: [
[
{
“node”: “HTTP - Fetch Page”,
“type”: “main”,
“index”: 0
}
]
]
},
“Has More Pages?”: {
“main”: [
[
{
“node”: “Delay”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “Aggregate All Data”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

The loop never ended. Even after the last id is not available.

Hi @rohanurrahman Welcome!

You can try this, as when the loop finishes (the IF goes false), connect that false branch to your next node and use {{$json.all_purchase_orders}}, which already contains all accumulated records from every page.