Code node output goes backwards and replaces previous node output

Describe the problem/error/question

I have been using n8n for the past year and haven’t had any issue like this one. I have 3 workflows set up to be executed in a sequence: first is triggered by a webhook call, then the following via execute workflow triggers. The first two workflows execute fine, but when execution reaches the third and final workflow something strange happens: when I check the execution of the workflow, I see that the output of the first code node has the output that should produce the third code node in that workflow! Then that output is treated as the “correct” output, so the following nodes process the incorrect data.

What is the error message (if any)?

There is no error message, since the workflow executes successfully.

Please share your workflow

Share the output returned by the last node

Since the aim of this flows is to process recipes for a diet creation app, the inputs and outputs of data are big so I’ll try to summarize it.
Workflow input:

{
 [
  "dietDays": [
    {
     "meal1":{
      "dish1":[
       {
        // Recipe objects
       },
       ...
      ],
      "dish2":[...],
      ...
     },
     "meal2":{...},
     ...
    }
   ]
 ]
}

A recipe object has been generated in previous workflow, as well as inserted to mongodb. It has a preparation, a list of ingredients, etc. Code node “Code” of this workflow gets some data from mongo (fields _id, meanMacros,etc) and appends them to current recipe objects.
Expected structure of a recipe object in “Code” output":

{
 "_id":"xxxx", // Appended by "Code" node
 "mean_macros": [], //Appended by "Code" node
 "mean_foods": [], //Appended by "Code" node
 "ingredients":[],
 "preparation":"",
 "name":"",
 ...
}

Current output of recipe object of “Code” node:

{
 "_id":"xxxx", // Appended by "Code" node, not modified
 "calories": 0, // Appended by "Code 2" node!!
 "foodQuant": [], // Appended by "Code 2" node!!
 // No more fields, meaning the deletion instructions in "Code 2" were executed
}

Things I have tried

  • I was able to know when the problem occurs by executing the workflow and producing errors or stopping the execution with a filter node. After a lot of attempts, it seems that if the flow reaches code node “Code 2” then its output is automatically assigned to output of code node “Code”. If the execution stops (because of errors or flow not reaching “Code 2”) then all nodes’ outputs are OK.
  • I’ve tried to delete the node and copying the code to a new node to “refresh it”.
  • I’ve tried to change the code of “Code 2”, the output assigned to node “Code” changes accordingly!
  • I’ve tried to change order of nodes: put “Item Lists 1” (which splits a collection) before “Code 2” and change code to run once per item. In this case, node “Code 2” was completely ignored by execution flow!
  • I’ve tried to pin data to a “Manual execution” node and connecting it to the different nodes. For some reason, I did not experience any issue whatsoever!
  • I’ve tried to move code nodes to the previous workflow. Did not work.
  • I’ve tried to replicate the workflow (no copying and pasting) into a freshly created workflow. Did not work.
  • I’ve tried restarting n8n.
  • The issue was detected while using n8n version 0.236, so I’ve also tried updating to 0.237, but this also did not work.

Information on your n8n setup

  • n8n version: 0.237.0
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: Rocky Linux 9.0

TL;DR

Output of node goes back in time and replaces output of first node, giving out strange results.

Welcome to the community @mdominguez!

I am sorry you are having trouble. Unfortunately I was not able to reproduce this on my end, and I suspect this is related to the input data being incomplete and me not having access to your MongoDB.

Would you be able to provide an example workflow allowing us to reproduce the problem without requiring access to your systems (for example by replacing the MongDB nodes with a dummy Code node returning hard-coded data)?

Also, can you confirm if this issue still occurs on the current version 1.5.1 of n8n for you?

Hello @MutedJam ! Thanks for your quick reply. It really is a strange behaviour so no doubt you were unable to reproduce it. In my opinion it must be something with the n8n database or the machine is running on. I’ll try to run the workflows in the latest version and keep you posted. For now I made a workaround and divided the problematic workflow in two, making the first one write its data onto a MongoDB collection and then triggering an external tool to check the collection and executing the second half of the workflow via webhook call. I’ll edit the main post with the example workflow as soon as I can.

1 Like

Hello @MutedJam

I have migrated n8n from 0.237.0 to version 1.4.1, and can confirm that I’m still having this issue. After making changes in the previous workflow the output changed again but right on the code node I made the changes! I have also tried

  • Changing the execution orded option from 0(legacy) to 1 (recommended)
  • Removing any cookies and cache fom my browser (even though I don’t think it had anything to do with that)

Hello,
We got the same problem on n8n version 1.3.1 running on Docker Desktop on a Windows VM.
The other Workflows working fine, when i launch the nodes one by one by hand, all works fine.
Even tried just to copy/paste the first 5 nodes (1 chron, 2 code nodes and 2 customNodes in it) into a new Workflow and it’s still the same case. Tried many more things, same as @mdominguez .
Without the customNodes, the problem still exists.

Hi @Mike_00, I am sorry you are having trouble. Can you please confirm if this is still happening on [email protected] using the new execution order, and if so share a workflow using which this can be reproduced (without requiring access to 3rd party systems and without using custom nodes)? Thanks!

Hi,
@MutedJam @mdominguez
It’s okay for me now, i found a workaround.

At the beginning of every code node, i used this row below to overcome the problem. I made a “copy” of the last result/array:

let newItems = JSON.parse(JSON.strnigify(items).all());

or

let newItems = JSON.parse(JSON.stringify($('NameOfTheWantedNode').all());

And then i worked in newItems.

For me this worked out.

1 Like

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