Skip to content

3. 筛选订单#

¥ Filtering Orders

在本工作流程步骤中,你将学习如何使用条件逻辑筛选数据,以及如何使用 如果节点 节点在节点中使用表达式。

¥In this step of the workflow, you will learn how to filter data using conditional logic and how to use expressions in nodes using the If node.

完成此步骤后,你的工作流应如下所示:

¥After this step, your workflow should look like this:

Workflow preview placeholder.

要仅将正在处理的订单插入 Airtable,我们需要使用 orderStatus 筛选数据。简而言之,我们希望告诉程序,如果 orderStatus 正在处理,则将所有具有此状态的记录插入 Airtable;例如,如果 orderStatus 未处理,则使用其他 orderStatus (booked) 计算所有订单的总和。

¥To insert only processing orders into Airtable we need to filter our data by orderStatus. Basically, we want to tell the program that if the orderStatus is processing, then insert all records with this status into Airtable; else, for example, if the orderStatus isn't processing, calculate the sum of all orders with the other orderStatus (booked).

此 if-then-else 命令是条件逻辑。在 n8n 工作流中,你可以使用 如果节点 添加条件逻辑,根据比较操作有条件地拆分工作流。

¥This if-then-else command is conditional logic. In n8n workflows, you can add conditional logic with the If node, which splits a workflow conditionally based on comparison operations.

If vs. Switch

如果你需要根据布尔值(true 和 false)以外的值过滤数据,请使用 切换节点 节点。Switch 节点类似于 If 节点,但支持多个输出连接器。

¥If you need to filter data on more than boolean values (true and false), use the Switch node. The Switch node is similar to the If node, but supports multiple output connectors.

添加 If 节点在 Airtable 节点之前#

¥Add If node before the Airtable node

首先,在 HTTP 请求节点和 Airtable 节点之间的连接中添加一个 If 节点:

¥First, let's add an If node between the connection from the HTTP Request node to the Airtable node:

  1. 将鼠标悬停在连接 HTTP 请求节点和 Airtable 节点的箭头上。

¥Hover over the arrow connection the HTTP Request node and the Airtable node. 2. 选择“HTTP 请求”节点和“Airtable”节点之间的“+”号。

¥Select the + sign between the HTTP Request node and the Airtable node.

配置 If 节点#

¥Configure the If node

选择加号会断开与 Airtable 节点到 HTTP 请求的连接。现在,让我们添加一个连接到 HTTP 请求节点的 If 节点:

¥Selecting the plus removes the connection to the Airtable node to the HTTP request. Now, let's add an If node connected to the HTTP Request node:

  1. 搜索 If 节点。

¥Search for the If node. 2. 在搜索结果中出现时点击。

¥Select it when it appears in the search.

对于 If 节点,我们将使用表达式。

¥For the If node, we'll use an expression.

Expressions

expression 是一串编程语言中的字符和符号,可以对其进行求值以获得一个值,通常根据其输入。在 n8n 工作流中,你可以在节点中使用表达式来引用另一个节点的输入数据。在我们的示例中,“如果”节点引用了“HTTP 请求”节点输出的数据。

¥An expression is a string of characters and symbols in a programming language that can be evaluated to get a value, often according to its input. In n8n workflows, you can use expressions in a node to refer to another node for input data. In our example, the If node references the data output by the HTTP Request node.

在“如果”节点窗口中,配置以下参数:

¥In the If node window, configure the parameters:

  • 按照以下步骤将 value1 占位符设置为 {{ $json.orderStatus }}

¥Set the value1 placeholder to {{ $json.orderStatus }} with the following steps: 1. 将鼠标悬停在 value1 字段上。

1
 ¥Hover over the value1 field.
  1. 选择“value1”字段右侧的“表达式”选项卡。

    ¥Select the Expression tab on the right side of the value1 field. 3. 接下来,点击链接图标打开表达式编辑器:

    ¥Next, open the expression editor by selecting the link icon:

    Opening the Expression Editor
    Opening the Expression Editor
    4. Use the left-side panel to select HTTP Request > orderStatus and drag it into the Expression field in the center of the window.
    Expression Editor in the IF node
    Expression Editor in the If node
    6. Once you add the expression, close the Edit Expression dialog.

  • 操作:选择“字符串 > 等于”

¥Operation: Select String > is equal to

  • value2 占位符设置为 processing

¥Set the value2 placeholder to processing.

Data Type

选择操作时,请务必选择正确的数据类型(布尔值、日期和时间、数字或字符串)。

¥Make sure to select the correct data type (boolean, date & time, number, or string) when you select the Operation.

选择“执行步骤”可测试“如果”节点。

¥Select Execute step to test the If node.

你的结果应如下所示:

¥Your results should look like this:

If node output
If node output

请注意,订单状态为 processing 的订单应显示在“真分支”输出中,而订单状态为 booked 的订单应显示在“假分支”输出中。

¥Note that the orders with a processing order status should show for the True Branch output, while the orders with a booked order status should show in the False Branch output.

完成后,关闭“如果”节点详细信息视图。

¥Close the If node detail view when you're finished.

将数据插入 Airtable#

¥Insert data into Airtable

接下来,我们需要将此数据插入 Airtable。还记得 Nathan 在 正在将数据插入 Airtable 课程结束时说的话吗?

¥Next, we want to insert this data into Airtable. Remember what Nathan said at the end of the Inserting data into Airtable lesson?

实际上,我只需要将正在处理的订单插入到表中……

¥I actually need to insert only processing orders in the table...

由于 Nathan 只需要表中的 processing 订单,我们将 Airtable 节点连接到 If 节点的 true 连接器。

¥Since Nathan only needs the processing orders in the table, we'll connect the Airtable node to the If node's true connector.

在这种情况下,由于 Airtable 节点已在画布上,请选择“如果节点 true”连接器并将其拖到 Airtable 节点。

¥In this case, since the Airtable node is already on our canvas, select the If node true connector and drag it to the Airtable node.

此时最好重新测试一下 Airtable 节点。操作前,请在 Airtable 中打开表格并删除所有现有行。然后在 n8n 中打开 Airtable 节点窗口,并选择“执行”步骤。

¥It's a good idea at this point to retest the Airtable node. Before you do, open your table in Airtable and delete all existing rows. Then open the Airtable node window in n8n and select Execute step.

检查你在 Airtable 中的数据,确保你的工作流仅添加了正确的订单(orderStatusprocessing 的订单)。现在应该有 14 条记录,而不是 30 条。

¥Review your data in Airtable to be sure your workflow only added the correct orders (those with orderStatus of processing). There should be 14 records now instead of 30.

此时,你的工作流程应如下所示:

¥At this stage, your workflow should look like this:

Workflow preview placeholder.

下一步是什么?#

¥What's next?

Nathan 🙋:If 节点对于过滤数据非常有用!现在我掌握了所有关于订单处理的信息。实际上,我只需要 employeeNameorderID,但我想我可以保留所有其他字段以防万一。

¥Nathan 🙋: This If node is so useful for filtering data! Now I have all the information about processing orders. I actually only need the employeeName and orderID, but I guess I can keep all the other fields just in case.

你 👩‍🔧:实际上,我不建议这样做。插入更多数据需要更多计算能力,数据传输速度更慢,耗时更长,并且会占用表中更多存储资源。在这个特定示例中,14 条记录和 5 个字段似乎不会造成显著差异,但如果你的业务增长到数千条记录和数十个字段,那么即使多一列也会影响性能。

¥You 👩‍🔧: Actually, I wouldn't recommend doing that. Inserting more data requires more computational power, the data transfer is slower and takes longer, and takes up more storage resources in your table. In this particular case, 14 records with 5 fields might not seem like it'd make a significant difference, but if your business grows to thousands of records and dozens of fields, things add up and even one extra column can affect performance.

Nathan 🙋:哦,这很有用。能否仅从处理顺序中选择两个字段?

¥Nathan 🙋: Oh, that's good to know. Can you select only two fields from the processing orders?

你 👩‍🔧:好的,我会在下一步中完成。

¥You 👩‍🔧: Sure, I'll do that in the next step.