Hello Community,
I have a workflow where my variables get messed up after the first iteration of a “Loop over Items” node. They stop working correctly and constantly return undefined when I try to use them later on.
My Workflow:Loop over Item → If Node → If True → Edit Field → back to Loop → If False → Edit Field 2 → back to Loop
The Issue: The IF condition only evaluates to true on the very first run, and after that, it always triggers false. The main problem is that Edit Field 2 (in the false branch) cannot access the variables set by the Edit Field node (in the true branch). They just return undefined.
Does anyone know how to keep these variables accessible across the different branches and loop iterations?
Hi @marexxxxxxx
I think you can try doing something like this in your set edit fields 2 where instead of referencing that edit fields node directly try using this $('Loop Over Items').item.json.yourVariable so that you can reference the data from the loop input, if you want the variable to be set in either branch, just make sure that both branches set the same field name so that downstream $json.someField can be picked up. And in case if the variable is set to true branch so ofc the edit field 2 in the false branch would not see it coming, so you can try passing down the original loop’s items data forward to use loop data as a common source.
Have you tried $('Loop Over Items').all()[0].json.Bild or by referencing the loop input data via $input.first().json.Bild inside the edit fields 2, and just make sure that both branches outputs the same field something like $json.Bild let me know if that works.
The issue here is not the reference syntax – it is how n8n branches work inside loops.
When the If node splits your items, the “true” path and “false” path run as separate parallel branches. Edit Fields (true branch) and Edit Fields 2 (false branch) never see each other’s output because they ran on different items. So $('Edit Fields').item.json.Bild in the false branch will always be undefined – that node literally did not run for those items.
Here is what is also happening with your If condition: after the first iteration, the output from Edit Fields and Edit Fields 2 gets merged back into the loop. On the second iteration, the IF node is re-evaluating based on the merged item data. If your condition references a field that gets modified in one of the branches, the condition behavior changes between iterations.
The architecture fix:
Use a single “Edit Fields” node that handles ALL cases, not one per branch. Reference the If result with a conditional expression:
Bild = {{ $('If').item.json.Bild ?? $('Loop Over Items').item.json.Bild }}
Or set Bild explicitly in both branches to the same field name, then reference $json.Bild downstream – but both branches MUST output that field or the downstream node will only receive items from one branch.
Simpler approach: skip the branching entirely. Use a single Edit Fields node after the If node with a conditional expression: