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:
-
MongoDB Node: Retrieves exactly 100 items successfully.
-
Loop Over Items Node: This is set to a batch size of 1 to process each item individually.
-
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
-
Why does the loop run this extra, empty time? Is this the intended and expected behavior?
-
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