Issue with Code Node on n8n Cloud (pro plan)

Describe the problem/error/question

The Code node (JavaScript) isn’t working as expected on our n8n Cloud pro plan. It isn’t able to access previous node outputs using $('NodeName').item.json.variable syntax.

I’ve tried drag-and-dropping the variable into the code, using autocomplete to generate the path in the code, and these both match (same code generated) - and the code intellisense agrees that the code is correct (no errors or red squiggly lines).

I’ve also tested this exact code successfully on a self-hosted n8n instance with no errors.

This code works if I remove the reference to $(‘Aggregate’). I have extensively tested this with other references from previous nodes as well - all are broken right now. (I’m a software engineer)

What is the error message (if any)?

“Unknown Error”

Error: Unknown error at JsTaskRunnerSandbox.throwExecutionError (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/JsTaskRunnerSandbox.js:55:19) at JsTaskRunnerSandbox.runCodeForEachItem (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/JsTaskRunnerSandbox.js:44:29) at processTicksAndRejections (node:internal/process/task_queues:95:5) at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:107:20) at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:681:27) at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:913:51 at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:1246:20

Please share your workflow

Share the output returned by the last node

The direct input:

[
  {
    "output": {
      "reasoning": "The email is a congratulatory message. It does not require personal attention as it is positive communication between colleagues. I will add the label 'Sorted by AI' for organization and remove 'INBOX' and 'IMPORTANT' labels accordingly. No other specific labels are applicable.",
      "add": [
        "Sorted by AI"
      ],
      "remove": [
        "INBOX",
        "IMPORTANT"
      ]
    }
  },
  {
    "output": {
      "reasoning": "The email is a follow-up regarding a technical aspect of a transfer which does not require personal attention. Therefore, the INBOX and IMPORTANT labels are removed. Labels 'Sorted by AI' will be added for organization. No specific labels like 'Leadership', 'Hiring & Recruiting', or 'Receipts' apply to this email.",
      "add": [
        "Sorted by AI"
      ],
      "remove": [
        "INBOX",
        "IMPORTANT"
      ]
    }
  },
  {},
  {
    "output": {
      "reasoning": "The email is a follow-up about Multi-factor authentication enforcement from a vendor, which is informational and doesn't require personal attention. Therefore, both 'INBOX' and 'IMPORTANT' labels can be removed. The 'Sorted by AI' label will be added to aid organization, along with 'System Alerts' as it relates to a security measure that may impact the user's access to services.",
      "add": [
        "Sorted by AI",
        "System Alerts"
      ],
      "remove": [
        "INBOX",
        "IMPORTANT"
      ]
    }
  },
  {
    "output": {
      "reasoning": "This email is discussing a product update. Since it doesn't require direct personal attention, the INBOX and IMPORTANT labels should be removed. The 'Sorted by AI' label needs to be added for organization purposes. No other relevant labels are applicable, resulting in a cleaner inbox.",
      "add": [
        "Sorted by AI"
      ],
      "remove": [
        "INBOX",
        "IMPORTANT"
      ]
    }
  }
]

And the $(‘Aggregate’) data:

[
  {
    "gmail_labels": [
      {
        "id": "CHAT",
        "name": "CHAT"
      },
      {
        "id": "SENT",
        "name": "SENT"
      },
      {
        "id": "INBOX",
        "name": "INBOX"
      },
      {
        "id": "IMPORTANT",
        "name": "IMPORTANT"
      },
      {
        "id": "TRASH",
        "name": "TRASH"
      },
      {
        "id": "DRAFT",
        "name": "DRAFT"
      },
      {
        "id": "SPAM",
        "name": "SPAM"
      },
      {
        "id": "CATEGORY_FORUMS",
        "name": "CATEGORY_FORUMS"
      },
      {
        "id": "CATEGORY_UPDATES",
        "name": "CATEGORY_UPDATES"
      },
      {
        "id": "CATEGORY_PERSONAL",
        "name": "CATEGORY_PERSONAL"
      },
      {
        "id": "CATEGORY_PROMOTIONS",
        "name": "CATEGORY_PROMOTIONS"
      },
      {
        "id": "CATEGORY_SOCIAL",
        "name": "CATEGORY_SOCIAL"
      },
      {
        "id": "STARRED",
        "name": "STARRED"
      },
      {
        "id": "UNREAD",
        "name": "UNREAD"
      },
      {
        "id": "Label_1623606469233510674",
        "name": "Receipts"
      },
      {
        "id": "Label_3076778126379818076",
        "name": "Industry News & Trends"
      },
      {
        "id": "Label_3322092101721916598",
        "name": "Customer Issues"
      },
      {
        "id": "Label_3828794275064901130",
        "name": "Newsletter"
      }
    ]
  }
]

Information on your n8n setup

  • n8n version: 1.83.2 (Stable)
  • Database (default: SQLite): Default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: n8n cloud

hello @mrmt

You can’t use .item.json reference for the Aggregate1 node, because the Aggregate node will always output 1 item and the next node Recent email1 may output more than 1 item. If there will be only 1 item, then it will work. Otherwise, you will get an error.

Use the $('Aggregate1').first().json.gmail_labels reference in the Code node

2 Likes

Thank you @barn4k, that did fix the issue!

Oh sorry, I take that back. The first() selector did fix the problem of the Code Node not executing, however after further testing it is still the broken .item selector that I need.

In my pasted example above I shortened the code to show how .item isn’t working, here’s the full code I’m trying to execute for context (below).

Essentially I’m purposely trying to iterate through the recent emails, and cross-filter labels against an approved list. The iteration is why .first() isn’t what I’m looking for - after testing, it’s only pulling in the first email instead of iterating through.

I do think this may be a bug with the current version of n8n cloud, since this exact code is working fine on a self-hosted n8n instance. (note the 'Aggregate1vsAggregate` and other variable differences are from me copy-pasting and scrubbing the example workflow above to remove sensitive details - the variables do match the actual workflow I’m running).

const addLabels = [];
for (let addIndex = 0; addIndex < $json.output.add.length; addIndex++) {
  addLabels.push($json.output.add[addIndex]);
}

const removeLabels = [];
for (
  let removeIndex = 0;
  removeIndex < $json.output.remove.length;
  removeIndex++
) {
  removeLabels.push($json.output.remove[removeIndex]);
}

const approvedLabels = $('Aggregate').first().json.gmail_labels;

const filteredAddLabels = addLabels
  .filter((addItem) => approvedLabels.find((o) => o.name === addItem))
  .map((addItem) => {
    const match = approvedLabels.find((o) => o.name === addItem);
    if (match) return { type: 'add', id: match.id };
  });
const filteredRemoveLabels = removeLabels
  .filter((removeItem) => approvedLabels.find((o) => o.name === removeItem))
  .map((removeItem) => {
    const match = approvedLabels.find((o) => o.name === removeItem);
    if (match) return { type: 'remove', id: match.id };
  });

return {
  id: $('Recent emails').item.json.id, // this part is breaking
  reasoning: $json.output.reasoning,
  actions: [...filteredAddLabels, ...filteredRemoveLabels],
};

I do see your point with the .item not necessarily lining up with the overall code node iteration. I’m going to fix the workflow by going back a step and having the $('Recent emails').item.json.id in the previous step $json.output instead, to sidestep the issue.

That seems to have worked! Thanks again for the help, the .item selector seems tricky to use haha.

1 Like

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