Loop over items issue

Hello n8n Community,

I have a workflow where I’m running into an issue with the Loop Over Items node. I’ve observed that for n input items, the loop executes n + 1 times. This final, extra iteration doesn’t contain any data and is causing a downstream node in my workflow to crash.

My Workflow & The Problem

Here’s the basic structure of my flow:

  1. MongoDB Node: Retrieves exactly 100 items successfully.

  2. Loop Over Items Node: This is set to a batch size of 1 to process each item individually.

  3. Code Node (or any other node): This node is inside the loop and needs to process data from the item.

The loop correctly processes the 100 items. However, it then runs a 101st time . In this final run, it passes an empty item to my Code Node .

Inside my Code Node , I have a line like this:

Generated javascript

// Get the call_id from the current item in the loop
const callId = $('Loop Over Items').item.json.entry_point_call_id;

This code works perfectly for the first 100 iterations. On the 101st iteration, however, $(‘Loop Over Items’).item.json is undefined, which causes the workflow to fail with the error: TypeError: Cannot read properties of undefined (reading ‘entry_point_call_id’).

My Questions

  1. Why does the loop run this extra, empty time? Is this the intended and expected behavior?

  2. What is the standard, best-practice way in n8n to prevent this final empty iteration from crashing the workflow?

I’m looking for the most robust and recommended solution for handling this. Thank you

Loop over Items always executes this way - one for each item provided and once more - when it comes back to the loop node (after the last item), where it determines no more input and the workflow takes the “done” branch.

1 Like