How to collect all data from nested loops for all runs

Describe the problem/error/question

I am trying to create a n8n workflow to search through reddit with a specific keyword. The idea is first to find all subreddits with the keyword and then go through each subreddit found to search for related posts. I am using nested loops.

Now the question is how to collect all the posts when the loops finish. For example, the outer loop runs 3 times, and the inner loop runs 4 times. I am expected 12 posts in total. In addition, I am applying some filtering, so the actual number of posts will be less.

With my current configuration, I can only get the data from the Node “Aggregate” for the first run.
I doubt it is related to the code node, but not sure how to fix.

Thanks for any help.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Stan_C

Nested loops are troublemakers, sub-loops won’t work as you expect, so you can only rely on one level of looping and in most cases you don’t actually need a second loop anyway because n8n iterates over your items by design.

However, if you still need another level of looping, the best approach is to move the second loop into a sub-workflow and call it using the Execute Workflow node, That way, each run of the outer loop starts a fresh inner loop, and everything works cleanly.


Now looking at your workflow, i do not think that you really need a second loop after reading your use case, here is how i’d do it:

and without even a loop:

1 Like