No data found for item-index: "1"

Hi!
I can’t understand why you can’t get the data from the preceding function item?

error
Error: No data found for item-index: "1"
    at Object.get (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/WorkflowDataProxy.js:119:31)
    at Object.get (<anonymous>)
    at Object.base.get (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:693:41)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:8:89
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:18:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1121:29)
    at Object.executeSingle (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/FunctionItem.node.js:72:33)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:474:60)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:416:62
node code
{
  "nodes": [
    {
      "parameters": {
        "queue": "tg_reply_to_message",
        "options": {
          "jsonParseBody": true,
          "onlyContent": true
        }
      },
      "name": "TG Reply To Message",
      "type": "n8n-nodes-base.rabbitmqTrigger",
      "typeVersion": 1,
      "position": [
        260,
        550
      ],
      "credentials": {
        "rabbitmq": "RabbitMQ n8n"
      }
    },
    {
      "parameters": {
        "dataType": "boolean",
        "value1": true,
        "rules": {
          "rules": [
            {
              "value2": "={{$node[\"TG Reply To Message\"].json[\"message\"][\"reply_to_message\"][\"text\"].includes(\"ID:\")}}"
            }
          ]
        }
      },
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        490,
        550
      ]
    },
    {
      "parameters": {
        "functionCode": "var result = {};\n\nvar re = new RegExp(\"#\", \"g\");\n\nvar message = $node[\"TG Reply To Message\"].json[\"message\"][\"text\"];\n\nvar tags = message.replace(re, \"\").split(\" \").map(String);\n\nresult[\"tags\"] = \"\";\nresult[\"insert_tags\"] = \"\";\nresult[\"select_tags\"] = \"(\";\n\nfor (var i = 0; i < tags.length; i++) {\n    result[\"tags\"] = result[\"tags\"] + \"#\" + tags[i] + \" \";\n    result[\"insert_tags\"] = result[\"insert_tags\"] + \"('\" + tags[i] + \"')\";\n    result[\"select_tags\"] = result[\"select_tags\"] + \"'\" + tags[i] + \"'\";\n    \n    if(tags.length - 1 > i) {\n        result[\"insert_tags\"] = result[\"insert_tags\"] + \", \"\n        result[\"select_tags\"] = result[\"select_tags\"] + \", \"\n    } else {\n        result[\"select_tags\"] = result[\"select_tags\"] + \")\"\n    }\n}\n\nvar text = $node[\"TG Reply To Message\"].json[\"message\"][\"reply_to_message\"][\"text\"];\n\nresult[\"id\"] = text.slice(4, 40);\n\nreturn result;\n"
      },
      "name": "BuildData",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        670,
        430
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=INSERT INTO notes_tags (name)\nVALUES {{$node[\"BuildData\"].json[\"insert_tags\"]}}\nON CONFLICT DO NOTHING;"
      },
      "name": "SaveTags",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        860,
        430
      ],
      "alwaysOutputData": true,
      "credentials": {
        "postgres": "PG_mr_braun"
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=SELECT id\nFROM notes_tags nt\nWHERE nt.name IN {{$node[\"BuildData\"].json[\"select_tags\"]}}"
      },
      "name": "GetTagIds",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        1050,
        430
      ],
      "alwaysOutputData": true,
      "credentials": {
        "postgres": "PG_mr_braun"
      }
    },
    {
      "parameters": {
        "functionCode": "var tag_ids = $node[\"GetTagIds\"].json[\"id\"].split(\",\");\n\nvar result = {};\n\nresult[\"insert_tags_link\"] = \"\";\n\nfor (var i = 0; i < tag_ids.length; i++) {\n    result[\"insert_tags_link\"] = result[\"insert_tags_link\"] + \"('\" + $node[\"BuildData\"].json[\"id\"] + \"', '\" + tag_ids[i] + \"')\";\n    \n    if(tag_ids.length - 1 > i) {\n        result[\"insert_tags_link\"] = result[\"insert_tags_link\"] + \", \"\n    }\n}\n\nreturn result;\n"
      },
      "name": "TagLinksData",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        1210,
        430
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=INSERT INTO notes_tag_link (notes_id, notes_tags_id)\nVALUES {{$node[\"TagLinksData\"].json[\"insert_tag_link\"]}}\nON CONFLICT DO NOTHING;"
      },
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        1420,
        430
      ],
      "credentials": {
        "postgres": "PG_mr_braun"
      },
      "disabled": true
    }
  ],
  "connections": {
    "TG Reply To Message": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "BuildData",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "BuildData": {
      "main": [
        [
          {
            "node": "SaveTags",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SaveTags": {
      "main": [
        [
          {
            "node": "GetTagIds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetTagIds": {
      "main": [
        [
          {
            "node": "TagLinksData",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TagLinksData": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

What does the GetTagIds node return? Can you change $node["BuildData"].json["id"] for $item(0).$node["BuildData"].json["id"] and try again?

2 Likes

GetTagIds gets the UUID of several objects in the database.
Your solution works.
Please explain why $item(0)? How is this index calculated?

The function $node uses the number of inputs to resolve to a value. The TagLinksData function is receiving multiple items but you are referencing the BuildData function which just outputs one item. Doing that you end up with something as below

BuildData Output

{
  "id": "123"
}

TagLinksData Input

[
   {
      "tag":1
   },
   {
      "tag":2
   },
   {
      "tag":3
   }
]
Index Function Resolve to
0 $node[“BuildData”].json[“id”] 123
1 $node[“BuildData”].json[“id”] undefined
2 $node[“BuildData”].json[“id”] undefined

I need to get one object to insert into Postgres, but I get an array and only one element of that array is inserted into Posgtres.

Can you please create another topic for that question? Thanks.

There seems to be many closed questions about this issue, however none of the discussions seems to resolve the problem on latest version of n8n on docker.

Error: ERROR: No data found for item-index: “1”

Example data:
[
{
“id”: “277070”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “279968”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “384964”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “386320”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “386322”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “386324”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “386326”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “386328”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “386329”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “419073”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “656523”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “663860”,
“date”: “2023-11-29 20:37:32”
},
{
“id”: “663861”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “663862”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “663863”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “663865”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “663867”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “751197”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “751202”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “751209”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “752630”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “753342”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “756037”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “812236”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “812244”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “812252”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “812295”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “223611”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “423716”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “423717”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “423718”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “423719”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “424137”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “424138”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “424139”,
“date”: “2023-11-29 20:37:33”
},
{
“id”: “424140”,
“date”: “2023-11-29 20:37:33”
}
]

Trying to loop each item on HTTP request node with the following variables (all of them return same error):

{{ $node[“Edit Fields2”].json[“id”] }}
{{ $[“Edit Fields2”].item.json[“id”] }}
{{ $node[“Edit Fields2”].json[“id”] }}
{{ $item(0).$node[“Edit Fields2”].json[“d”] }}
{{ $item($itemIndex).$node[“Edit Fields2”].json[“id”] }}
{{ $json.id }}
{{ $json.item.id }}

The previous node contains 36 items, so it should be possible to loop through the items and run http request for each item?