Function node doesn't accept returned array

Hi everyone!

I have been trying to use the function node to filter the input json from another node for a specific object and return a value id that corresponds to an object id.

n8n keeps telling me that I need to return an Array of items, while this is what I am doing (to my understanding).

Would be very grateful for any help here.

Error: No data got returned. Always return an Array of items!
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:71:19)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:395:47
const valueToFind=248103;                    
for (let i=0;i<items[0].length;i++) {            
     if(items[0][i].field_id==valueToFind) {  
        return items[0][i].value.id               
         }
 }

You currently seem to only return a single value if you find it from the array, but you need to return it in an array. You could do it like this:

const valueToFind=248103;                    
for (let i=0;i<items[0].length;i++) {            
     if(items[0][i].field_id==valueToFind) {  
        return [{json: {id: items[0][i].value.id}}];            
         }
 }
return [{json: {}}] // Didn't find anything, so return empty item

EDIT: Item’s data is in the json property, so even that wouldn’t really work as it expects items[0] to be an array. What does your input data look like?

2 Likes

Hi mluhta,

thank you for the help.
This is my input.

[
  [
    {
      "id": 340736919,
      "field_id": 249841,
      "list_entry_id": 8730686,
      "entity_type": 1,
      "value_type": 3,
      "entity_id": 221906118,
      "value": 1
    },
    {
      "id": 340736920,
      "field_id": 249842,
      "list_entry_id": 8730686,
      "entity_type": 1,
      "value_type": 3,
      "entity_id": 221906118,
      "value": 1
    },
    {
      "id": 340736921,
      "field_id": 249844,
      "list_entry_id": 8730686,
      "entity_type": 1,
      "value_type": 4,
      "entity_id": 221906118,
      "value": "2018-06-18T00:00:00.000-07:00"
    },
    {
      "id": 340736922,
      "field_id": 249846,
      "list_entry_id": 8730686,
      "entity_type": 1,
      "value_type": 4,
      "entity_id": 221906118,
      "value": "2018-06-18T00:00:00.000-07:00"
    },
    {
      "id": 331654839,
      "field_id": 176786,
      "list_entry_id": null,
      "entity_type": 1,
      "value_type": 0,
      "entity_id": 221906118,
      "value": 20013506
    },
    {
      "id": 327636234,
      "field_id": 249843,
      "list_entry_id": 8730686,
      "entity_type": 1,
      "value_type": 6,
      "entity_id": 221906118,
      "value": "https://www.linkedin.com/company/test-lead-gen"
    },
    {
      "id": 341720308,
      "field_id": 239963,
      "list_entry_id": null,
      "entity_type": 1,
      "value_type": 6,
      "entity_id": 221906118,
      "value": "https://eu2.salesforce.com/xx"
    },
    {
      "id": 341720310,
      "field_id": 299833,
      "list_entry_id": null,
      "entity_type": 1,
      "value_type": 6,
      "entity_id": 221906118,
      "value": "https://www.linkedin.com/company/test-lead-gen"
    },
    {
      "id": 899619044,
      "field_id": 239949,
      "list_entry_id": null,
      "entity_type": 1,
      "value_type": 0,
      "entity_id": 221906118,
      "value": 20013506
    },
    {
      "id": 341720309,
      "field_id": 248103,
      "list_entry_id": null,
      "entity_type": 1,
      "value_type": 7,
      "entity_id": 221906118,
      "value": {
        "id": 1193432,
        "text": "Active Conversation",
        "rank": 4,
        "color": 3
      }
    }
  ]
]

Okay, this should work:

const valueToFind=248103;
for (let i=0; i < items[0].json.length; i++)
  if (items[0].json[i].field_id == valueToFind)
    return [{json: {id: items[0].json[i].id}}];
return [{json: {}}] // Didn't find anything, so return empty item

I tested it with this workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json: [\n    {\n      \"id\": 340736919,\n      \"field_id\": 249841,\n      \"list_entry_id\": 8730686,\n      \"entity_type\": 1,\n      \"value_type\": 3,\n      \"entity_id\": 221906118,\n      \"value\": 1\n    },\n    {\n      \"id\": 340736920,\n      \"field_id\": 249842,\n      \"list_entry_id\": 8730686,\n      \"entity_type\": 1,\n      \"value_type\": 3,\n      \"entity_id\": 221906118,\n      \"value\": 1\n    },\n    {\n      \"id\": 340736921,\n      \"field_id\": 249844,\n      \"list_entry_id\": 8730686,\n      \"entity_type\": 1,\n      \"value_type\": 4,\n      \"entity_id\": 221906118,\n      \"value\": \"2018-06-18T00:00:00.000-07:00\"\n    },\n    {\n      \"id\": 340736922,\n      \"field_id\": 249846,\n      \"list_entry_id\": 8730686,\n      \"entity_type\": 1,\n      \"value_type\": 4,\n      \"entity_id\": 221906118,\n      \"value\": \"2018-06-18T00:00:00.000-07:00\"\n    },\n    {\n      \"id\": 331654839,\n      \"field_id\": 176786,\n      \"list_entry_id\": null,\n      \"entity_type\": 1,\n      \"value_type\": 0,\n      \"entity_id\": 221906118,\n      \"value\": 20013506\n    },\n    {\n      \"id\": 327636234,\n      \"field_id\": 249843,\n      \"list_entry_id\": 8730686,\n      \"entity_type\": 1,\n      \"value_type\": 6,\n      \"entity_id\": 221906118,\n      \"value\": \"https://www.linkedin.com/company/test-lead-gen\"\n    },\n    {\n      \"id\": 341720308,\n      \"field_id\": 239963,\n      \"list_entry_id\": null,\n      \"entity_type\": 1,\n      \"value_type\": 6,\n      \"entity_id\": 221906118,\n      \"value\": \"https://eu2.salesforce.com/xx\"\n    },\n    {\n      \"id\": 341720310,\n      \"field_id\": 299833,\n      \"list_entry_id\": null,\n      \"entity_type\": 1,\n      \"value_type\": 6,\n      \"entity_id\": 221906118,\n      \"value\": \"https://www.linkedin.com/company/test-lead-gen\"\n    },\n    {\n      \"id\": 899619044,\n      \"field_id\": 239949,\n      \"list_entry_id\": null,\n      \"entity_type\": 1,\n      \"value_type\": 0,\n      \"entity_id\": 221906118,\n      \"value\": 20013506\n    },\n    {\n      \"id\": 341720309,\n      \"field_id\": 248103,\n      \"list_entry_id\": null,\n      \"entity_type\": 1,\n      \"value_type\": 7,\n      \"entity_id\": 221906118,\n      \"value\": {\n        \"id\": 1193432,\n        \"text\": \"Active Conversation\",\n        \"rank\": 4,\n        \"color\": 3\n      }\n    }\n  ]}]"
      },
      "name": "Test Input",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        410,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const valueToFind=248103;\nfor (let i=0; i < items[0].json.length; i++)\n  if (items[0].json[i].field_id == valueToFind)\n    return [{json: {id: items[0].json[i].id}}];\nreturn [{json: {}}] // Didn't find anything, so return empty item\n"
      },
      "name": "Filter",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        610,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Test Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test Input": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Yes, it worked indeed.
Thank you so much!