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?

1 Like

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.