Skip to content

表达式#

¥Expressions

表达式是所有 n8n 节点中实现的一项强大功能。它们允许根据以下数据动态设置节点参数:

¥Expressions are a powerful feature implemented in all n8n nodes. They allow node parameters to be set dynamically based on data from:

  • 先前节点执行次数

¥Previous node executions

  • 工作流

¥The workflow

  • 你的 n8n 环境

¥Your n8n environment

你还可以在表达式中执行 JavaScript,从而方便快捷地将数据转换为有用的参数值,而无需编写大量额外的代码。

¥You can also execute JavaScript within an expression, making this a convenient and easy way to manipulate data into useful parameter values without writing extensive extra code.

n8n 创建并使用名为 锦标赛 的模板语言,并使用 自定义方法和变量数据转换函数 对其进行扩展。这些功能可以更轻松地执行常见任务,例如从其他节点获取数据或访问工作流元数据。

¥n8n created and uses a templating language called Tournament, and extends it with custom methods and variables and data transformation functions. These features make it easier to perform common tasks like getting data from other nodes or accessing workflow metadata.

n8n 还支持两个库:

¥n8n additionally supports two libraries:

  • Luxon,用于处理日期和时间。

¥Luxon, for working with dates and time.

¥JMESPath, for querying JSON.

Data in n8n

编写表达式时,了解 n8n 中的数据结构和行为会很有帮助。有关在工作流中处理数据的更多信息,请参阅 数据

¥When writing expressions, it's helpful to understand data structure and behavior in n8n. Refer to Data for more information on working with data in your workflows.

编写表达式#

¥Writing expressions

要使用表达式设置参数值:

¥To use an expression to set a parameter value:

  1. 将鼠标悬停在要使用表达式的参数上。

¥Hover over the parameter where you want to use an expression. 2. 在“固定/表达式”切换开关中选择“表达式”。

¥Select Expressions in the Fixed/Expression toggle. 3. 在参数中输入表达式,或选择“打开表达式编辑器”Open expressions editor icon 打开表达式编辑器。如果你使用表达式编辑器,则可以在变量选择器中浏览可用数据。所有表达式的格式均为 {{ your expression here }}

¥Write your expression in the parameter, or select Open expression editor {.off-glb} to open the expressions editor. If you use the expressions editor, you can browse the available data in the Variable selector. All expressions have the format {{ your expression here }}.

示例:从 Webhook 获取数据正文#

¥Example: Get data from webhook body

考虑以下场景:你有一个 Webhook 触发器,它通过 Webhook 正文接收数据。你希望提取部分数据以供工作流使用。

¥Consider the following scenario: you have a webhook trigger that receives data through the webhook body. You want to extract some of that data for use in the workflow.

你的 Webhook 数据类似于以下内容:

¥Your webhook data looks similar to this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[
  {
    "headers": {
      "host": "n8n.instance.address",
      ...
    },
    "params": {},
    "query": {},
    "body": {
      "name": "Jim",
      "age": 30,
      "city": "New York"
    }
  }
]

在工作流的下一个节点中,你需要获取 city 的值。你可以使用以下表达式:

¥In the next node in the workflow, you want to get just the value of city. You can use the following expression:

{{$json.body.city}}

此表达式:

¥This expression:

  1. 使用 n8n 的自定义 $json 变量访问传入的 JSON 格式数据。

¥Accesses the incoming JSON-formatted data using n8n's custom $json variable. 2. 查找 city 的值(在本例中为 "纽约")。请注意,此示例使用 JMESPath 语法查询 JSON 数据。你还可以将此表达式写成 {{$json['body']['city']}}

¥Finds the value of city (in this example, "New York"). Note that this example uses JMESPath syntax to query the JSON data. You can also write this expression as {{$json['body']['city']}}.

示例:编写更长的 JavaScript 代码#

¥Example: Writing longer JavaScript

你可以在表达式中进行变量赋值或使用多个语句,但需要使用立即调用函数表达式 (IIFE) 的语法封装代码。

¥You can do things like variable assignments or multiple statements in an expression, but you need to wrap your code using the syntax for an IIFE (Immediately Invoked Function Expression).

以下代码使用 Luxon 日期和时间库来计算两个日期之间的月份差。我们将代码用 Handlebar 括号括起来,用于表达式和 IIFE 语法。

¥The following code use the Luxon date and time library to find the time between two dates in months. We surround the code in both the handlebar brackets for an expression and the IIFE syntax.

{{(()=>{ let end = DateTime.fromISO('2017-03-13'); let start = DateTime.fromISO('2017-02-13'); let diffInMonths = end.diff(start, 'months'); return diffInMonths.toObject(); })()}}

常见问题#

¥Common issues

有关表达式的常见错误或问题以及建议的解决方法,请参阅 常见问题

¥For common errors or issues with expressions and suggested resolution steps, refer to Common Issues.