n8n 中的循环#
¥Looping in n8n
循环在处理多个项目或重复执行操作时非常有用,例如向通讯录中的每个联系人发送消息。n8n 会自动处理这种重复性操作,这意味着你无需在工作流程中专门构建循环。有 部分节点 个用户不符合此条件。
¥Looping is useful when you want to process multiple items or perform an action repeatedly, such as sending a message to every contact in your address book. n8n handles this repetitive processing automatically, meaning you don't need to specifically build loops into your workflows. There are some nodes where this isn't true.
在 n8n 中使用循环#
¥Using loops in n8n
n8n 节点接受任意数量的输入项,处理这些输入项,并输出结果。你可以将每个项目视为一个数据点,或者节点输出表中的一行。
¥n8n nodes take any number of items as input, process these items, and output the results. You can think of each item as a single data point, or a single row in the output table of a node.
节点通常每个项目运行一次。例如,如果你想将客户数据存储节点中的客户名称和备注作为消息发送到 Slack,你可以:
¥Nodes usually run once for each item. For example, if you wanted to send the name and notes of the customers in the Customer Datastore node as a message on Slack, you would:
- 将 Slack 节点连接到“客户数据存储”节点。
¥Connect the Slack node to the Customer Datastore node. 2. 配置参数。
¥Configure the parameters. 3. 执行节点。
¥Execute the node.
你将收到五条消息:每个项目一个。
¥You would receive five messages: one for each item.
这样你就可以在不显式连接循环中的节点的情况下处理多个项目。
¥This is how you can process multiple items without having to explicitly connect nodes in a loop.
执行节点一次#
¥Executing nodes once
如果你不希望某个节点处理所有接收到的数据项,例如仅向第一个客户发送 Slack 消息,你可以通过在该节点的“设置”选项卡中切换“执行一次”参数来实现。当传入数据包含多个数据项,而你只想处理第一个数据项时,此设置非常有用。
¥For situations where you don't want a node to process all received items, for example sending a Slack message only to the first customer, you can do so by toggling the Execute Once parameter in the Settings tab of that node This setting is helpful when the incoming data contains multiple items and you want to only process the first one.
创建循环#
¥Creating loops
n8n 通常处理所有传入项的迭代。但是,在某些情况下,你需要创建一个循环来遍历所有项目。请参阅 节点例外 获取不会自动遍历所有传入项的节点列表。
¥n8n typically handles the iteration for all incoming items. However, there are certain scenarios where you will have to create a loop to iterate through all items. Refer to Node exceptions for a list of nodes that don't automatically iterate over all incoming items.
循环直到满足条件#
¥Loop until a condition is met
要在 n8n 工作流中创建循环,请将一个节点的输出连接到前一个节点的输入。添加一个 IF 节点来检查何时停止循环。
¥To create a loop in an n8n workflow, connect the output of one node to the input of a previous node. Add an IF node to check when to stop the loop.
以下是一个实现了包含 IF 节点的循环的 示例工作流 示例:
¥Here is an example workflow that implements a loop with an IF node:
循环直到处理完所有项目#
¥Loop until all items are processed
当你需要循环处理所有项目时,请使用 循环处理项目 节点。要单独处理每个项目,请将批处理大小设置为 1。
¥Use the Loop Over Items node when you want to loop until all items are processed. To process each item individually, set Batch Size to 1.
你可以将数据分组并进行批量处理。当处理大量传入数据或需要处理特定组返回项时,此方法有助于避免 API 速率限制。
¥You can batch the data in groups and process these batches. This approach is useful for avoiding API rate limits when processing large incoming data or when you want to process a specific group of returned items.
循环处理项节点会在所有传入项被分成批次并传递给工作流中的下一个节点后停止执行,因此无需添加 IF 节点来停止循环。
¥The Loop Over Items node stops executing after all the incoming items get divided into batches and passed on to the next node in the workflow so it's not necessary to add an IF node to stop the loop.
节点例外#
¥Node exceptions
需要在工作流中设计循环的节点和操作:
¥Nodes and operations where you need to design a loop into your workflow:
- CrateDB 对
insert和update执行一次。
¥CrateDB executes once for insert and update.
- 代码 节点处于“一次运行,所有项”模式:根据输入的代码片段处理所有项目。
¥Code node in Run Once for All Items mode: processes all the items based on the entered code snippet.
- 执行工作流 节点处于“一次运行,所有项”模式。
¥Execute Workflow node in Run Once for All Items mode.
- HTTP 请求:你必须自行处理分页。如果你的 API 调用返回分页结果,则必须创建一个循环来一次获取一页。
¥HTTP Request: you must handle pagination yourself. If your API call returns paginated results you must create a loop to fetch one page at a time.
- Microsoft SQL 对
insert、update和delete执行一次。
¥Microsoft SQL executes once for insert, update, and delete.
- MongoDB 对
insert和update执行一次。
¥MongoDB executes once for insert and update.
- QuestDB 对
insert执行一次。
¥QuestDB executes once for insert.
-
信息:无论传入数据项的数量如何,此操作都只执行一次。
¥Info: this operation executes only once, regardless of the number of items in the incoming data.
-
RSS 读取 对请求的 URL 执行一次。
¥RSS Read executes once for the requested URL.
- TimescaleDB 对
insert和update执行一次。
¥TimescaleDB executes once for insert and update.

