I need in a WF a nested loop (I get items, then on each item I need to retrieve data and do multiple things).
But I can’t figure how it’s working. So different than other languages.
I can’t understand how to do that.
Could someone explain how to achieve this ?
Why doesn’t this WF just execute the main loop three times and, inside it, execute the nested loop twice?
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:
n8n
February 17, 2025, 6:54pm
2
It looks like your topic is missing some important information. Could you provide the following if applicable.
n8n version:
Database (default: SQLite):
n8n EXECUTIONS_PROCESS setting (default: own, main):
Running n8n via (Docker, npm, n8n cloud, desktop app):
Operating system:
What I can’t understand, is that it enter the second loop only once.
If I put reset option, I run forever.
If I add a condition {{ $prevNode = ‘Code 1’ }} same thing, the loop is executed only once.
Is it a bug ? Or is there no way to nest loops with n8n ? Or maybe I don’t understand the logic behind this…
What are the options?
barn4k
February 18, 2025, 7:45am
5
hello @presta_melt
You can’t use nested loops this way. You have to move the inner/outer Loop functionality into a sub-workflow
OK will do this. But just can’t understand why it’s not possible to do a so simple thing…
1 Like
barn4k
February 18, 2025, 7:59am
7
A sub-workflow is simplier than the loop node actually. Because the nodes by design loops over items.
But nested loop is a troublemaker, and it simply doesn’t work as intended because there are only $runIndex
(increased for every loop node’s iteration) and $itemIndex
(increased for every node item within the current $runIndex
). The nested loop will behave oddly as the $runIndex
is already changed by the outer loop node.
OK.
I find this limitation unfortunate; it’s a very simple thing that we often need. But okay, now I have your answer, so I’ll stop wasting time on this.
Maybe it should be flagged as bug or a feature request?
BTW I managed to do it work with a conditionnal reset {{ $prevNode.name === ‘Code1’ }}
2 Likes
system
Closed
February 25, 2025, 8:04am
9
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.