Skip to content

代码节点常见问题(Code node common issues)#

以下是 Code 节点 的一些常见错误和问题,以及解决或排查它们的步骤。

🌐 Here are some common errors and issues with the Code node and steps to resolve or troubleshoot them.

代码未正确返回项(Code doesn't return items properly)#

当你的代码节点中的代码未按预期格式返回数据时,会发生此错误。

🌐 This error occurs when the code in your Code node doesn't return data in the expected format.

在 n8n 中,节点之间传递的所有数据都是对象数组。这些对象中的每一个都用 json 键封装了另一个对象:

🌐 In n8n, all data passed between nodes is an array of objects. Each of these objects wraps another object with the json key:

1
2
3
4
5
6
7
[
  {
    "json": {
	  // your data goes here
	}
  }
]

要解决此错误,请检查以下内容:

🌐 To troubleshoot this error, check the following:

  • 阅读数据结构以了解在代码节点中接收的数据以及从节点输出数据的要求。
  • 了解数据项的工作原理以及如何通过item linking将前一个节点的数据项连接起来。

'json' 属性不是对象(A 'json' property isn't an object)#

当代码节点返回的数据中 json 键未指向对象时,会发生此错误。

🌐 This error occurs when the Code node returns data where the json key isn't pointing to an object.

如果你将 json 设置为不同的数据结构,比如数组,就可能发生这种情况:

🌐 This may happen if you set json to a different data structure, like an array:

1
2
3
4
5
6
7
[
  {
    "json": [
	  // Setting `json` to an array like this will produce an error
	]
  }
]

为了解决这个问题,请确保 json 键引用的是你返回数据中的一个对象:

🌐 To resolve this, ensure that the json key references an object in your return data:

1
2
3
4
5
6
7
[
  {
    "json": {
	  // Setting `json` to an object as expected
	}
  }
]

代码未返回对象(Code doesn't return an object)#

当你的代码节点没有返回任何内容或返回了意外结果时,可能会出现此错误。

🌐 This error may occur when your Code node doesn't return anything or if it returns an unexpected result.

为了解决此问题,请确保你的代码节点返回预期的数据结构

🌐 To resolve this, ensure that your Code node returns the expected data structure:

1
2
3
4
5
6
7
[
  {
    "json": {
	  // your data goes here
	}
  }
]

如果你提供的代码返回 'undefined' 而不是预期结果,也可能会发生此错误。在这种情况下,请确保你在代码节点中引用的数据在每次执行中都存在,并且具有代码所期望的结构。

🌐 This error may also occur if the code you provided returns 'undefined' instead of the expected result. In that case, ensure that the data you are referencing in your Code node exists in each execution and that it has the structure your code expects.

'import' 和 'export' 只能出现在顶层('import' and 'export' may only appear at the top level)#

如果你尝试在 Code 节点中使用 importexport,就会发生此错误。它们不受 n8n 的 JavaScript 沙箱支持。相反,请使用 require 函数来加载模块。

🌐 This error occurs if you try to use import or export in the Code node. These aren't supported by n8n's JavaScript sandbox. Instead, use the require function to load modules.

为了解决此问题,请尝试将你的 import 语句改为使用 require

🌐 To resolve this issue, try changing your import statements to use require:

1
2
3
4
// Original code:
// import express from "express";
// New code:
const express = require("express");

找不到模块 '<module>'(Cannot find module '<module>')#

如果你在 Code 节点中尝试使用 require,但 n8n 找不到该模块,就会发生此错误。

🌐 This error occurs if you try to use require in the Code node and n8n can't find the module.

仅适用于自托管

n8n 在 Cloud 版本中不支持导入模块。

如果你正在 自托管 n8n,请按照以下步骤操作:

🌐 If you're self-hosting n8n, follow these steps:

  • 将模块安装到你的 n8n 环境中。
    • 如果你使用 npm 运行 n8n,请在与 n8n 相同的环境中安装该模块。
    • 如果你使用 Docker 运行 n8n,你需要用包含你的模块的 自定义镜像 扩展官方 n8n 镜像。
  • 设置 NODE_FUNCTION_ALLOW_BUILTINNODE_FUNCTION_ALLOW_EXTERNAL 环境变量 以允许导入模块。

使用全局变量(Using global variables)#

有时你可能希望设置和获取与工作流相关的简单全局数据,无论是在执行之间还是执行过程中。例如,在编写包含项目更新列表的报告时,你可能希望包含上一份报告的日期。

🌐 Sometimes you may wish to set and retrieve simple global data related to a workflow across and within executions. For example, you may wish to include the date of the previous report when compiling a report with a list of project updates.

要直接向工作流设置、更新和检索数据,请在代码中使用静态数据函数。你可以全局管理数据,也可以将其绑定到特定节点。

🌐 To set, update, and retrieve data directly to a workflow, use the static data functions within your code. You can manage data either globally or tied to specific nodes.

尽可能使用删除重复项

如果你希望通过使用变量来避免对相同的数据项进行多次处理,可以考虑使用删除重复节点。删除重复节点可以在多次执行之间保存信息,从而避免对相同的数据项进行多次处理。