循环处理项目#
¥Loop Over Items
循环处理项节点可帮助你在需要时循环处理数据。
¥The Loop Over Items node helps you loop through data when needed.
该节点保存原始传入数据,并在每次迭代中通过循环输出返回预定义量的数据。
¥The node saves the original incoming data, and with each iteration, returns a predefined amount of data through the loop output.
节点执行完成后,它会将所有处理的数据合并,并通过“完成”输出返回。
¥When the node execution completes, it combines all of the processed data and returns it through the done output.
何时使用“循环遍历项目”节点#
¥When to use the Loop Over Items node
默认情况下,n8n 节点设计用于处理输入项列表(少数例外情况详见下文)。根据你想要实现的目标,你的流程中通常不需要“循环遍历项目”节点。你可以在 n8n 中的循环 页面上了解更多关于 n8n 如何处理多个项目的信息。
¥By default, n8n nodes are designed to process a list of input items (with some exceptions, detailed below). Depending on what you're trying to achieve, you often don't need the Loop Over Items node in your workflow. You can learn more about how n8n processes multiple items on the looping in n8n page.
以下链接重点介绍了“循环遍历项目”节点的一些应用场景:
¥These links highlight some of the cases where the Loop Over Items node can be useful:
- 循环直到处理完所有项目:描述“循环处理项目”节点与常规项目处理的区别,以及何时可能需要使用此节点。
¥Loop until all items are processed: describes how the Loop Over Items node differs from normal item processing and when you might want to incorporate this node.
- 节点例外:概述你可能需要使用“循环遍历项目”节点手动构建循环逻辑的具体情况和节点。
¥Node exceptions: outlines specific cases and nodes where you may need to use the Loop Over Items node to manually build looping logic.
- 避免速率限制:演示如何批量处理 API 请求以避免其他服务的速率限制。
¥Avoiding rate limiting: demonstrates how to batch API requests to avoid rate limits from other services.
节点参数#
¥Node parameters
批处理大小#
¥Batch Size
请输入每次调用返回的项数。
¥Enter the number of items to return with each call.
节点选项#
¥Node options
重置#
¥Reset
如果启用,节点将在每次循环时使用新初始化的当前输入数据重置。当希望“循环遍历项目”节点将传入数据视为一组新数据,而不是先前项目的延续时,请使用此选项。
¥If turned on, the node will reset with the current input-data newly initialized with each loop. Use this when you want the Loop Over Items node to treat incoming data as a new set of data instead of a continuation of previous items.
例如,如果你事先不知道需要多少页,可以使用带有重置选项的“循环遍历项目”节点,并将 如果节点 设置为 如果节点 来查询分页服务。循环逐页查询,执行任何处理,并递增页码。循环重置确保循环将每次迭代识别为一组新的数据。“如果”节点评估退出条件,以决定是否执行另一次迭代。
¥For example, you can use the Loop Over Items node with the reset option and an If node to query a paginated service when you don't know how many pages you need in advance. The loop queries pages one at a time, performs any processing, and increments the page number. The loop reset ensures the loop recognizes each iteration as a new set of data. The If node evaluates an exit condition to decide whether to perform another iteration or not.
Include a valid termination condition
对于类似上述示例的工作流,必须为循环添加有效的终止条件。如果你的终止条件始终不匹配,则工作流执行将陷入无限循环。
¥For workflows like the example described above, it's critical to include a valid termination condition for the loop. If your termination condition never matches, your workflow execution will get stuck in an infinite loop.
启用后,你可以通过将参数表示形式从“固定”切换到“表达式”来调整重置条件。表达式求值的结果决定了节点何时重置项目处理。
¥When enabled, you can adjust the reset conditions by switching the parameter representation from Fixed to Expression. The results of your expression evaluation determine when the node will reset item processing.
模板和示例#
¥Templates and examples
从两个不同的源读取 RSS 源#
¥Read RSS feed from two different sources
此工作流允许你使用“循环遍历项目”节点从两个不同的源读取 RSS 源。你需要在工作流中使用“循环遍历项目”节点,因为“RSS 源读取”节点只会处理接收到的第一个项目。你还可以在以下位置找到 workflow: n8n.io
¥This workflow allows you to read an RSS feed from two different sources using the Loop Over Items node. You need the Loop Over Items node in the workflow as the RSS Feed Read node only processes the first item it receives. You can also find the workflow on n8n.io.
该示例将引导你完成工作流的构建,但假设你已经熟悉 n8n。要构建你的第一个工作流(包括学习如何向工作流添加节点),请参阅 试用。
¥The example walks through building the workflow, but assumes you are already familiar with n8n. To build your first workflow, including learning how to add nodes to a workflow, refer to Try it out.
最终工作流程如下所示:
¥The final workflow looks like this:
Workflow preview placeholder.
复制上面的工作流文件并粘贴到你的实例中,或者按照以下步骤手动构建:
¥Copy the workflow file above and paste into your instance, or manually build it by following these steps:
- 添加手动触发器。
¥Add the manual trigger. 2. 添加代码节点。
¥Add the Code node. 3. 将此代码复制到代码节点中:
¥Copy this code into the Code node:
1 2 3 4 5 6 7 8 9 10 11 12 | |
¥Add the Loop Over Items node.
5. 配置循环遍历项目:在“批处理大小”字段中将批处理大小设置为 1。
¥Configure Loop Over Items: set the batch size to 1 in the Batch Size field.
6. 添加 RSS 源读取节点。
¥Add the RSS Feed Read node. 7. 选择“执行工作流”。此操作会运行工作流,将数据加载到 RSS 源读取节点。
¥Select Execute Workflow. This runs the workflow to load data into the RSS Feed Read node.
8. 配置 RSS 源读取:将 url 从输入映射到 URL 字段。你可以通过从“输入”面板拖放,或使用以下表达式来完成此操作:{{ $json.url }}。
¥Configure RSS Feed Read: map url from the input to the URL field. You can do this by dragging and dropping from the INPUT panel, or using this expression: {{ $json.url }}.
9. 选择“执行工作流”可运行工作流并查看结果数据。
¥Select Execute Workflow to run the workflow and see the resulting data.
检查节点是否已处理所有项目#
¥Check that the node has processed all items
要检查节点是否仍有待处理项,请使用以下表达式:{{$node["Loop Over Items"].context["noItemsLeft"]}}。此表达式返回一个布尔值。如果节点仍有数据需要处理,则表达式返回 false,否则返回 true。
¥To check if the node still has items to process, use the following expression: {{$node["Loop Over Items"].context["noItemsLeft"]}}. This expression returns a boolean value. If the node still has data to process, the expression returns false, otherwise it returns true.
获取节点的当前运行索引#
¥Get the current running index of the node
要获取节点的当前运行索引,请使用以下表达式:{{$node["Loop Over Items"].context["currentRunIndex"];}}。
¥To get the current running index of the node, use the following expression: {{$node["Loop Over Items"].context["currentRunIndex"];}}.