Why Does n8n Use Last Loop’s Empty all_messages?

I’m building a workflow in n8n to fetch all messages from a Facebook conversation using the Facebook Graph API, accumulate them into an all_messages array across multiple loops, and then format them once the loop completes. The loop itself works fine while fetching paginated data, but when it exits, the “Combine message” node only receives the all_messages value from the final loop iteration—which is empty—instead of the full accumulated array from all previous iterations.

Here’s what I’m trying to do:

  1. Start with an empty current_after and fetch the first 50 messages.

  2. Append each batch of messages to all_messages in a “SetCursor” node during each loop iteration.

  3. Update current_after from paging.cursors.after and loop back if there’s more data (controlled by an “If” node).

  4. Exit the loop when there’s no more data (is_complete becomes true) and pass the accumulated all_messages to the “Combine message” node.

The issue is that after the loop ends, the “Combine message” node only gets the all_messages from the last iteration (which is empty because the final API call returns no new messages), and I lose all the messages accumulated from previous iterations.

Information n8n setup

  • n8n version: 1.78.1

  • Database (default: SQLite): SQLite (default)

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

  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm (local deployment)

  • Operating system: [Please fill in your OS, e.g., Windows 10, macOS, Ubuntu 20.04, etc.]

I suspect this might be related to how n8n handles data between loop iterations or how the “If” node passes data to the next node after exiting the loop. How can I ensure that the full all_messages array, accumulated across all iterations, is passed to the “Combine message” node instead of just the last iteration’s empty array? Any suggestions would be greatly appreciated!

That’s how a normal variable would work in normal code. Unfortunately variables are not available in self-hosted n8n instances.

Every time you declare a new JSON field in n8n, it’s like creating the variable for the first time.

But the loop node should output all the processed items to the “Done” branch.

Changing your logic will solve this. You just can’t rely on a static variable for it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.