Function node / items not defined

Hi!

I suppose this question has been answered before, but I am struggling to figure it out myself atm. I changed the mode of a Code node to “Run once for each item” and am getting a “items is not defined” error. This is on n8n cloud.

This is the code:

const { url, title, short_summary, long_summary, websiteTags, type } = JSON.parse(items[0].json.message.content);
const permalink = $node["Slack_get_permalink"].json["permalink"];
const posted_by = $node["Slack_get_user_info"].json.user["real_name"];
const tagsArray = websiteTags.split(',').filter(tag => tag.trim() !== '').map(tag => ({ name: tag.trim() }));

return [
  {
    json: {
      parent: {
        type: "database_id",
        database_id: "857a90a3a06c442eaeccda241ef88005"
      },
      properties: {
        "Titel": { 
          title: [
            {
              text: {
                content: title
              }
            }
          ]
        },
        "%5DPYM": { //Zusammenfasung
          "type": "rich_text",
          "rich_text": [
            {
              "type": "text",
              "text": {
                "content": short_summary,
                "link": null
              },
            }
          ]
        },
        "dyS%3F": { //URL  
          "type": "url",
          "url": url
        },
        "KVr%3D": { //Gepostet von 
          "type": "rich_text",
          "rich_text": [
            {
              "type": "text",
              "text": {
                "content": posted_by,
                "link": null
              },
            }
          ]
        },
        "%40FmA": { //Typ
          "type": "select",
          "select": {
            "name": type,
          }
        },
        "W__w": { //Tags
          "type": "multi_select",
          "multi_select": tagsArray
        },
        "I%5CDt": { //Slack URL  
          "type": "url",
          "url": permalink
        },
        "eVKI": { //Lange Zusammenfasung
          "type": "rich_text",
          "rich_text": [
            {
              "type": "text",
              "text": {
                "content": long_summary,
                "link": null
              },
            }
          ]
        }
      }
    },
  }
];

And this is the input:

[
  {
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "CONTENT"
    },
    "finish_reason": "stop"
  },
  {
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "CONTENT"
    },
    "finish_reason": "stop"
  }
]

Any hints as to how to change the code for the new mode? Thanks a lot!

You probably have to just change items[0] to $input.item.

Meaning the first line should be:

const { url, title, short_summary, long_summary, websiteTags, type } = JSON.parse($input.item.json.message.content);

More information about how to use the Code node can be found here:
https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.code

And about the available variables here:
https://docs.n8n.io/code/builtin/
or the specific input ones here:
https://docs.n8n.io/code/builtin/current-node-input/

Hi Jan,

Thanks a lot. That solved one problem and I also got an error saying the code doesn’t return an object, so I fixed that as well. Now I am getting an error about the input data:

ERROR: “1” [line 2, for item 1]
No data found for item-index

Input data as the same as above, this is the new code:

const { url, title, short_summary, long_summary, websiteTags, type } = JSON.parse($input.item.json.message.content);
const permalink = $node[“Slack_get_permalink”].json[“permalink”];
const posted_by = $node[“Slack_get_user_info”].json.user[“real_name”];
const tagsArray = websiteTags.split(‘,’).filter(tag => tag.trim() !== ‘’).map(tag => ({ name: tag.trim() }));

return {
json: {
parent: {
type: “database_id”,
database_id: “857a90a3a06c442eaeccda241ef88005”
},
properties: {
“Titel”: {
title: [
{
text: {
content: title
}
}
]
},
“%5DPYM”: { //Zusammenfasung
“type”: “rich_text”,
“rich_text”: [
{
“type”: “text”,
“text”: {
“content”: short_summary,
“link”: null
},
}
]
},
“dyS%3F”: { //URL
“type”: “url”,
“url”: url
},
“KVr%3D”: { //Gepostet von
“type”: “rich_text”,
“rich_text”: [
{
“type”: “text”,
“text”: {
“content”: posted_by,
“link”: null
},
}
]
},
“%40FmA”: { //Typ
“type”: “select”,
“select”: {
“name”: type,
}
},
“W__w”: { //Tags
“type”: “multi_select”,
“multi_select”: tagsArray
},
“I%5CDt”: { //Slack URL
“type”: “url”,
“url”: permalink
},
“eVKI”: { //Lange Zusammenfasung
“type”: “rich_text”,
“rich_text”: [
{
“type”: “text”,
“text”: {
“content”: long_summary,
“link”: null
},
}
]
}
}
}
};

Thanks!

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