Nested Loop over item doesn't work properly

Describe the problem/error/question

It looks like the nested loop over items does not work properly. Or I don’t understand how it works.
For example, I have a very simple workflow. The input data is JSON:

[
  {
    "name": "First item",
    "code": 1,
    "nodes": [
      {
        "id": 1
      },
      {
        "id": 2
      }
    ]
  },
  {
    "name": "Second item",
    "code": 2,
    "nodes": [
      {
        "id": 3
      },
      {
        "id": 4
      }
    ]
  }
]

My target is to print to console all IDs (1, 2, 3, and 4).
I’ve created a loop for items and a nested loop for going through all id’s one by one

Also, I have Code blocks with prints to the console.

I simplified the workflow because in ьн real complex workflow I have exactly the same problem with nested loops

What is the error message (if any)?

When I execute my workflow I expect that I will get the following result in DevTools console:

[Node: "Code1"] =====================
[Node: "Code1"] Code: 1
[Node: "Code3"] Id: 1
[Node: "Code3"] Id: 2
[Node: "Code1"] =====================
[Node: "Code1"] Code: 2
[Node: "Code3"] Id: 3
[Node: "Code3"] Id: 4

But in fact, I got the result:

[Node: "Code1"] =====================
[Node: "Code1"] Code: 1
[Node: "Code3"] Id: 1
[Node: "Code3"] Id: 2
[Node: "Code1"] =====================
[Node: "Code1"] Code: 2

Please share your workflow

Share the output returned by the last node

It is strange that on the third step, I see IDs 3 and 4 on the ‘Done’ branch in the second Loop element



Information on your n8n setup

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

Thanks in advance!

Hey @Roman_Garanin,

Welcome to the community :cake:

Can you share the example worklfow you are using? A lot of the time you won’t need the split in batches node as we loop items automatically but looking at your data you have 2 items and in that you have another 2 so you may need to extract the nodes array first to work on that.

Hi @Jon
Thank you for your answer
My workflow:

Yes, I understand that I can modify my workflows to avoid nested loops. In my case, I can just collect subarray into one array and process it without nested loops.
But sometimes it is impossible to do so. It is just an example. I have a case where I can rich my target only with nested loops and it doesn’t work properly.
Could you explain to me why it works wrong in the provided case? I suggest that it is correct but I don’t understand why :slightly_smiling_face:

Try this:

1 Like

@shrey-42

{{ $node['Loop Over Items1'].context["done"] }}

Yes, it works!
Thanks a lot!

However, it was not obvious to me. For example, documentation pages

and examples pages do not contain information about context[“done”]

Where did you find this solution? Maybe you know where I can find detailed information about ‘context’ array?

In my opinion, it is important information and it could be added this information to common documentation.

In any case thanks! :handshake: :+1:

1 Like

Hey @Roman_Garanin,

We mention the conext option here: Loop Over Items | n8n Docs and briefly here: Output of other nodes | n8n Docs but we don’t specifically mention the done option. It does look like we need to update the page as it using the older syntax as well.

1 Like

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