How to use N8N to nest a loop within a loop

Describe the problem/error/question

When I use N8n to nest a loop within a loop, after my child loop node finishes running, it starts executing the second iteration of the parent loop node. However, when it triggers the child loop node again, it defaults to only executing the “done” phase and skips the “loop” phase.

What is the error message (if any)?

FIRST FATHER LOOP:

BUT THE SECOND FATHER LOOP:

Please share your workflow

{
“nodes”: [
{
“parameters”: {
“jsCode”: “const contents = $(‘DeepSeek1’).all();\nlet inputText = ;\n\nconsole.log(‘Contents:’, contents); // 调试信息\n\nfor (let i = 0; i < contents.length; i++) {\n console.log(‘Processing item:’, i); // 调试信息\n if (contents[i].json && contents[i].json.message && contents[i].json.message.content) {\n console.log(‘Content found:’, contents[i].json.message.content); // 调试信息\n inputText.push(contents[i].json.message.content);\n } else {\n console.log(‘Content not found for item:’, i); // 调试信息\n }\n}\n\nconsole.log(‘Input Text:’, inputText); // 调试信息\n\n// 用来存储最终的输出项\nlet outputItems = ;\n\n// 正则表达式匹配每一行,包括 #、##、###、- 和其他段落\nconst lines = inputText.join(‘\n’).split(‘\n’);\n\n// 当前段落存储变量\nlet currentText = ‘’;\nlet currentType = ‘paragraph’; // 默认类型是paragraph\n\n// 逐行处理文本\nlines.forEach(line => {\n // 移除多余的空格\n const trimmedLine = line.trim();\n\n // 如果该行是标题\n if (trimmedLine.startsWith('# ‘)) {\n // 处理当前段落并推送\n if (currentText) {\n outputItems.push({ text: currentText.trim(), type: currentType });\n }\n currentText = trimmedLine.replace(’# ‘, ‘’); // 去掉 # 符号\n currentType = ‘heading_1’; // 设置类型为 heading_1\n } else if (trimmedLine.startsWith(’## ‘)) {\n if (currentText) {\n outputItems.push({ text: currentText.trim(), type: currentType });\n }\n currentText = trimmedLine.replace(’## ‘, ‘’); // 去掉 ## 符号\n currentType = ‘heading_2’; // 设置类型为 heading_2\n } else if (trimmedLine.startsWith(’### ‘)) {\n if (currentText) {\n outputItems.push({ text: currentText.trim(), type: currentType });\n }\n currentText = trimmedLine.replace(’### ‘, ‘’); // 去掉 ### 符号\n currentType = ‘heading_3’; // 设置类型为 heading_3\n } else if (trimmedLine.startsWith(’- ‘)) {\n if (currentText) {\n outputItems.push({ text: currentText.trim(), type: currentType });\n }\n currentText = trimmedLine.replace(’- ', ‘’); // 去掉 - 符号\n currentType = ‘bulleted_list_item’; // 设置类型为 bulleted_list_item\n } else if (trimmedLine) {\n // 处理常规段落\n if (currentText) {\n outputItems.push({ text: currentText.trim(), type: currentType });\n }\n currentText = trimmedLine;\n currentType = ‘paragraph’; // 设置类型为 paragraph\n }\n});\n\n// 最后一次匹配完成后,处理剩余的内容\nif (currentText) {\n outputItems.push({ text: currentText.trim(), type: currentType });\n}\n\n// 输出结果\nreturn outputItems.map(item => ({ json: item }));”
},
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
-2900,
2020
],
“id”: “1f7997fa-d3da-4581-a7f8-7dc96832057b”,
“name”: “分段分类1”,
“alwaysOutputData”: false
},
{
“parameters”: {
“options”: {}
},
“type”: “n8n-nodes-base.splitInBatches”,
“typeVersion”: 3,
“position”: [
-2560,
2020
],
“id”: “2ad71468-b84e-4c66-917b-a99ea6539312”,
“name”: “轮循1”,
“retryOnFail”: true
},
{
“parameters”: {
“rules”: {
“values”: [
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“leftValue”: “={{ $(‘分段分类1’).item.json.type }}”,
“rightValue”: “heading_1”,
“operator”: {
“type”: “string”,
“operation”: “equals”
}
}
],
“combinator”: “and”
},
“renameOutput”: true,
“outputKey”: “heading_1”
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “be6a7a47-4362-4d7c-a105-9a55776a5d17”,
“leftValue”: “={{ $(‘分段分类1’).item.json.type }}”,
“rightValue”: “heading_2”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
},
“renameOutput”: true,
“outputKey”: “heading_2”
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “d66f787f-9248-4a94-81ef-2f75f72503fc”,
“leftValue”: “={{ $(‘分段分类1’).item.json.type }}”,
“rightValue”: “heading_3”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
},
“renameOutput”: true,
“outputKey”: “heading_3”
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “3ecc08e6-3a1d-4667-80c4-37ef0873cdc5”,
“leftValue”: “={{ $(‘分段分类1’).item.json.type }}”,
“rightValue”: “bulleted_list_item”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
},
“renameOutput”: true,
“outputKey”: “bulleted_list_item”
},
{
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 2
},
“conditions”: [
{
“id”: “0ba72b7a-6f10-4b90-bdba-1c0eba95e0c5”,
“leftValue”: “={{ $(‘分段分类1’).item.json.type }}”,
“rightValue”: “paragraph”,
“operator”: {
“type”: “string”,
“operation”: “equals”,
“name”: “filter.operator.equals”
}
}
],
“combinator”: “and”
},
“renameOutput”: true,
“outputKey”: “paragraph”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.switch”,
“typeVersion”: 3.2,
“position”: [
-2320,
1980
],
“id”: “371de0dc-d5c2-4a1f-8611-b7c6d0c8927e”,
“name”: “语句分类1”
},
{
“parameters”: {
“resource”: “block”,
“blockId”: {
“__rl”: true,
“value”: “={{ $(‘创建博客1’).item.json.url }}”,
“mode”: “url”
},
“blockUi”: {
“blockValues”: [
{
“textContent”: “=文章发布于公众号:云原生安全Hub,https://mp.weixin.qq.com/s/UPxP09ih8LLTKA-FXtwsjQ
}
]
}
},
“type”: “n8n-nodes-base.notion”,
“typeVersion”: 2.2,
“position”: [
-2280,
1700
],
“id”: “2cd6a9f5-7987-4d1e-934d-48d67ac05fa3”,
“name”: “追加末尾公众号”,
“executeOnce”: true,
“retryOnFail”: true,
“credentials”: {
“notionApi”: {
“id”: “juR1BGJv5CRbYjjC”,
“name”: “Notion account”
}
}
},
{
“parameters”: {
“model”: “deepseek-chat”,
“prompt”: {
“messages”: [
{
“role”: “system”,
“content”: “=你是一个云安全工具开发师 。针对这一个工具,请严格按以下的格式排版:# "工具名称":- :link:工具链接:工具链接### :bar_chart::- :pushpin:核心内容:- :key:重要信息:- :bulb:实用价值:- :wrench:技术栈: - :jigsaw:集成与扩展: - :dart:示例使用场景: - :rocket:未来扩展: ,标注关键参数注意:- 输出格式统一,上面的每个小标题的内容要写两三个点,内容需要翻译为中文 除此之外其他都不要写-\n你需要处理的信息为:{{ $json.readme_content }}”
}
]
},
“options”: {},
“requestOptions”: {}
},
“type”: “n8n-nodes-deepseek.deepSeek”,
“typeVersion”: 1,
“position”: [
-3460,
2020
],
“id”: “c9411954-c387-48c6-9d2e-3e291e181702”,
“name”: “DeepSeek1”,
“notesInFlow”: true,
“credentials”: {
“deepSeekApi”: {
“id”: “ai2GFeDI88jsP43K”,
“name”: “DeepSeek account”
}
}
},
{
“parameters”: {
“options”: {}
},
“type”: “n8n-nodes-base.splitInBatches”,
“typeVersion”: 3,
“position”: [
-4220,
2000
],
“id”: “afc43642-6ab4-4f6d-8525-77409751d5b3”,
“name”: “Loop Over Items”
},
{
“parameters”: {
“url”: “={{ $json.url}}”,
“options”: {}
},
“id”: “c1eae832-88c5-4ef9-ac26-726c54fdc586”,
“name”: “HTTP Request1”,
“type”: “n8n-nodes-base.httpRequest”,
“typeVersion”: 4.2,
“position”: [
-3920,
2020
]
},
{
“parameters”: {
“operation”: “extractHtmlContent”,
“extractionValues”: {
“values”: [
{
“key”: “readme_content”,
“cssSelector”: “article.markdown-body”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.html”,
“typeVersion”: 1.2,
“position”: [
-3700,
2020
],
“id”: “f29b6fe4-a8f9-4a3e-ab1d-3360cce52af5”,
“name”: “HTML”
},
{
“parameters”: {
“resource”: “databasePage”,
“databaseId”: {
“__rl”: true,
“value”: “1390f0c5b87c80f1a2daf38d2bc4f74c”,
“mode”: “id”
},
“title”: “Cybersercurity”,
“propertiesUi”: {
“propertyValues”: [
{
“key”: “category|select”,
“selectValue”: “=技术分享”
},
{
“key”: “date|date”,
“date”: “={{ $now.format(‘yyyy/MM/dd’) }}”
},
{
“key”: “slug|rich_text”,
“textContent”: “={{ $(‘Loop Over Items’).item.json.url }}”
},
{
“key”: “status|select”,
“selectValue”: “Published”
},
{
“key”: “tags|multi_select”,
“multiSelectValue”: [
“工具”
]
},
{
“key”: “title|title”,
“title”: “={{ $(‘Loop Over Items’).item.json.url }}工具介绍”
},
{
“key”: “type|select”,
“selectValue”: “Post”
},
{
“key”: “summary|rich_text”,
“textContent”: “={{ $(‘Loop Over Items’).item.json.url }}工具介绍”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.notion”,
“typeVersion”: 2.2,
“position”: [
-3220,
2020
],
“id”: “0d1d9598-88c0-4e5b-a07b-d9ee5ca5b3ea”,
“name”: “创建博客1”,
“executeOnce”: true,
“retryOnFail”: true,
“notesInFlow”: true,
“credentials”: {
“notionApi”: {
“id”: “juR1BGJv5CRbYjjC”,
“name”: “Notion account”
}
}
}
],
“connections”: {
“分段分类1”: {
“main”: [
[
{
“node”: “轮循1”,
“type”: “main”,
“index”: 0
}
]
]
},
“轮循1”: {
“main”: [
[
{
“node”: “追加末尾公众号”,
“type”: “main”,
“index”: 0
}
],
[
{
“node”: “语句分类1”,
“type”: “main”,
“index”: 0
}
]
]
},
“语句分类1”: {
“main”: [
,
,
,
,

]
},
“追加末尾公众号”: {
“main”: [

]
},
“DeepSeek1”: {
“main”: [
[
{
“node”: “创建博客1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Loop Over Items”: {
“main”: [
,
[
{
“node”: “HTTP Request1”,
“type”: “main”,
“index”: 0
}
]
]
},
“HTTP Request1”: {
“main”: [
[
{
“node”: “HTML”,
“type”: “main”,
“index”: 0
}
]
]
},
“HTML”: {
“main”: [
[
{
“node”: “DeepSeek1”,
“type”: “main”,
“index”: 0
}
]
]
},
“创建博客1”: {
“main”: [
[
{
“node”: “分段分类1”,
“type”: “main”,
“index”: 0
}
]
]
}
},
“pinData”: {}
}

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite): sqlite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: windows 10

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
  • **n8n version: 1.71.2
  • **Database (default: SQLite):SQLite
  • **n8n EXECUTIONS_PROCESS setting (default: own, main):own
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • **Operating system: windows10

Hey @cdxiaodong , nesting Loops is not a good idea as it could lead to unexpected results. Also each loop has to be closed (last node of the loop has to be routed back to the beginning for the Loop node). However, if you do need to feed more than one set of items into the same Loop node you need to reset it before feeding the next set of items as explained in Loop Node Only Processes the First Item - How to Fix? - #12 by Jon.

Finally,

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.