Loop and nested loop fail to iterate through items

Describe the issue/error/question

I have a workflow based on consuming data from multiple SeaTable tables and creating the appropriate folder structure as well as accompanying JSON files, and downloads files hosted by SeaTable. The workflow is by no means complete. I currently have one loop nested inside another. I will have to nest another loop inside that nested loop.

The issues:

  1. The first loop (For Each Course) receives 3 items through the previous validator node but only iterates through the first 2.
  2. The nested loop (For Each Lesson) receives 5 items from the Lessons table (3 for first course and 2 for the second) but only iterates through the first 3.

What is the error message (if any)?

The workflow completes without error, but does not iterate through the data as would be expected.

Please share the workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: latest
  • Database you’re using (default: SQLite): default
  • Running n8n with the execution process [own(default), main]: own
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

Hi @markinator, welcome to the community :tada:

I’m sorry to hear you’re having trouble. Unfortunately I am not able to run your workflow since it seems to rely on data only available to you.

Is there a chance you simply need to adjust the order in which you connect your loops? This might be a bit counterintuitive but I posted a simplified example a while back here which hopefully makes things a bit easier to understand:

I still get the same result but I am not entirely sure I connected the nodes in the correct order.

In the example you showed it’s the node before the IF node that gets connected back to the Split In Batches node, however in examples I found elsewhere in the forum it was the IF node that got looped around to the Split In Batches node.

Can you have a look at the screenshot and help me out with which nodes to disconnect and the order to reconnect them?

Yes, connecting both outputs of the IF node in the example would make things simpler indeed, but it seemed like a good example to visualize things here.

But generally speaking, my suggestion here would be to start with a much smaller workflow. Build a single loop, something like this:

Example Workflow

Make sure it works as expected, then add the functionality of the inner loop step by step, retesting the workflow frequently. Once you’re happy with the inner loop, build the outer loop around it.

I sure can take a closer look at your workflow if you feel stuck but I won’t know when that would be tbh seeing you have provided a beast of a flow that can’t simply be executed and played around with :wink:

Thanks for the guidance, it’s greatly appreciated. I will try to troubleshoot my way out of this and post a solution if I find one.

1 Like

I have discovered that the Split In Batches node does not reset currentRunIndex for every iteration of a nested loop while keeping the maxRunIndex at the value of the first iteration.

For example let’s say the first parent loop sends the following number of items in each iteration:

  1. 3 items
  2. 1 item
  3. 2 items

Then at the end of each iteration of the nested loop currentRunIndex will be:

  1. 3
  2. 4
  3. 6

Since maxRunIndex of the first iteration was 3, all subsequent iterations have a value higher than that and return false when the condiition is set to: currentRunIndex < maxRunIndex

Excuse the noobish way of creating the data structure at the start. I don’t know how to make it in a function.