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:

2 Likes

Yes, it worked indeed.
Thank you so much!