Nested Loops Impossible :-(

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:

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:

1.78.0, / self-hosted

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?

hello @presta_melt

You can’t use nested loops this way. You have to move the inner/outer Loop functionality into a sub-workflow

:frowning:

OK will do this. But just can’t understand why it’s not possible to do a so simple thing…

1 Like

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

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