Skip to content

代码节点常见问题#

¥Code node common issues

以下是 代码节点 的一些常见错误和问题,以及解决或故障排除步骤。

¥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:

  • 阅读 数据结构 文档,了解你在代码节点中接收的数据以及从该节点输出数据的要求。

¥Read the data structure to understand the data you receive in the Code node and the requirements for outputting data from the node.

  • 了解数据项的工作原理以及如何将先前节点的数据项与 项目链接 连接起来。

¥Understand how data items work and how to connect data items from previous nodes with 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

如果你尝试在“代码”节点中使用 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");

找不到模块 '<模块>'#

¥Cannot find module '<module>'

如果你尝试在“代码”节点中使用 require,而 n8n 找不到该模块,则会出现此错误。

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

Only for self-hosted

n8n 不支持导入 版本的模块。

¥n8n doesn't support importing modules in the Cloud version.

如果你是 self-hosting n8n,请按照以下步骤操作:

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

  • 将模块安装到你的 n8n 环境中。

¥Install the module into your n8n environment.

  • 如果你使用 npm 运行 n8n,请在与 n8n 相同的环境中安装该模块。

    ¥If you are running n8n with npm, install the module in the same environment as n8n.

  • 如果你使用 Docker 运行 n8n,则需要使用包含你的模块的 自定义镜像 扩展官方 n8n 镜像。

    ¥If you are running n8n with Docker, you need to extend the official n8n image with a custom image that includes your module.

  • 设置 NODE_FUNCTION_ALLOW_BUILTINNODE_FUNCTION_ALLOW_EXTERNAL 环境变量 以允许导入模块。

¥Set the NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL environment variables to allow importing modules.

使用全局变量#

¥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.

Use Remove Duplicates when possible

如果你希望使用变量来避免多次处理相同的数据项,请考虑使用 移除重复节点。“删除重复项”节点可以跨执行保存信息,避免多次处理相同的项目。

¥If you're interested in using variables to avoid processing the same data items more than once, consider using the Remove Duplicates node instead. The Remove Duplicates node can save information across executions to avoid processing the same items multiple times.