Can't get SplitInBatches to work with Function node

I have a function Node, and by using the following code:

const PrintfulPayload = {
 
vars: items[0].json.vars
  
};

PrintfulPayload 

return [
  {
    json: PrintfulPayload 
  }
];

I am able to get:

[
   {
      "vars":[
         {
            "id":6834,
            "product_id":5,
           
         },
         {
            "id":6835,
            "product_id":5,
         }
      ]
   }
]

But the SplitInBatches seems to need the data without the array like:

[
   {
         {
            "id":6834,
            "product_id":5,
           
         },
         {
            "id":6835,
            "product_id":5,
            
         }
   }
]

If I try and get it without, like:

const PrintfulPayload = {
 
 
 items[0].json.vars
  
};

PrintfulPayload 

return [
  {
    json: PrintfulPayload 
  }
]; 

It throws an error. How do I get just a list of the array items?

Hey @gravitoad!

The output from the Function node is returning an object which contains the array. The SplitInBatches node takes this as a single input and returns the whole object as the output. Instead of returning a single object, you want to return multiple objects. Similar to the following example

[
         {
            "id":6834,
            "product_id":5,
           
         },
         {
            "id":6835,
            "product_id":5,
         }
]

Below is an example that might help. The first two function nodes are creating the mock data. You want to use the Create JSON-items function node.

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return items[0].json.vars.map(item => { \n  return {\n    json: item,\n  }\n})\n"
      },
      "name": "Create JSON-items",
      "type": "n8n-nodes-base.function",
      "position": [
        835,
        320
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      vars: $items().map(item => item.json),\n    },\n  }\n];\n"
      },
      "name": "Create an array of objects",
      "type": "n8n-nodes-base.function",
      "position": [
        630,
        320
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      data: 'item-1',\n    }\n  },\n  {\n    json: {\n      data: 'item-2',\n    }\n  },\n  {\n    json: {\n      data: 'item-3',\n    }\n  }\n];"
      },
      "name": "Mock Data1",
      "type": "n8n-nodes-base.function",
      "position": [
        450,
        300
      ],
      "typeVersion": 1
    }
  ],
  "connections": {
    "Create an array of objects": {
      "main": [
        [
          {
            "node": "Create JSON-items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock Data1": {
      "main": [
        [
          {
            "node": "Create an array of objects",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this helps! :slightly_smiling_face:

2 Likes

That did the trick, thanks a bunch!

1 Like

I am happy that this worked! Have fun! :slightly_smiling_face: