How to return array of items

Hi, I’m very new to n8n. My coding skills are very basic though.
I have function that calculates difference between parsed dates. For example I get this JSON data from previous nodes(3 items):

[
{
"UID": 2,
"USER_ID": 8593847591,
"USERNAME": "John",
"PUBLISH_DATE": 1641789802000,
"CURRENT_DATE": 1642058123000
},
{
"UID": 8,
"USER_ID": 59384595045,
"USERNAME": "Susan",
"PUBLISH_DATE": 1641952418000,
"CURRENT_DATE": 1642058123000
},
{
"UID": 9,
"USER_ID": 9504832934,
"USERNAME": "Chris",
"PUBLISH_DATE": 1641836651000,
"CURRENT_DATE": 1642058123000
}
]

and after function I get only 1 item instead of 3 calculations. I understand that I need to use array, but I don’t know how to do it correctly and I get an error:
“Always an Array of items has to be returned!”

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "var date1 = $node[\"calcdate\"].json.PUBLISH_DATE;\nvar date2 = $node[\"calcdate\"].json.CURRENT_DATE;\n\nvar Difference_In_Time = date2 - date1;\n\nvar Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);\n\nvar result = 14-Math.floor(Difference_In_Days);\n\nreturn [{\n  json: { \"diff\": result}\n}]\n "
      },
      "name": "datecalc",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1660,
        100
      ]
    }
  ],
  "connections": {}
}

Hi @vlrpsknv, welcome to the community!

I am not sure I fully understand your question. When running your example Function code I don’t get the Always an Array of items has to be returned! error you have described and your data structure seems to be correct:

Do you want to perform this calculation (CURRENT_DATE minus PUBLISH_DATE) for each input item? If so, you might want to use the Function Item node instead of the Function node as this would run once for each input item. Here is an example workflow demonstrating the idea:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const example = [\n{\n\"UID\": 2,\n\"USER_ID\": 8593847591,\n\"USERNAME\": \"John\",\n\"PUBLISH_DATE\": 1641789802000,\n\"CURRENT_DATE\": 1642058123000\n},\n{\n\"UID\": 8,\n\"USER_ID\": 59384595045,\n\"USERNAME\": \"Susan\",\n\"PUBLISH_DATE\": 1641952418000,\n\"CURRENT_DATE\": 1642058123000\n},\n{\n\"UID\": 9,\n\"USER_ID\": 9504832934,\n\"USERNAME\": \"Chris\",\n\"PUBLISH_DATE\": 1641836651000,\n\"CURRENT_DATE\": 1642058123000\n}\n];\n\nreturn example.map(e => {return {json: e} })"
      },
      "name": "calcdate",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "var date1 = item.PUBLISH_DATE;\nvar date2 = item.CURRENT_DATE;\n\nvar Difference_In_Time = date2 - date1;\n\nvar Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);\n\nvar result = 14-Math.floor(Difference_In_Days);\n\nitem[\"diff\"] = result;\n\nreturn item;"
      },
      "name": "datecalc_per_item",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        660,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "calcdate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "calcdate": {
      "main": [
        [
          {
            "node": "datecalc_per_item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

1 Like