Grab Results from Different Node Into a Function

Hi experts,
I was wondering how I can merge different nodes into a Function, I know there is an element called Merge, but in my example I’d like to know how I can do it using a function, thus, perform filters by JS and make the function return something more customized.

Thanks

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -1330,
        2640
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    postal_codes: ['LA96RA','DA145DL','PH263AE','ERROR','BH118QX','IG80SU','BD108LZ','TA116LA','GL538AN','TS69DQ','GU342DS']\n  }\n}];"
      },
      "name": "Set Postal Codes1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -1120,
        2640
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "postal_codes",
        "options": {}
      },
      "name": "Split Out Postal Codes1",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        -910,
        2640
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatchesPOSTCODE",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -660,
        2640
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"SplitInBatchesPOSTCODE\"].context[\"noItemsLeft\"]}}",
              "operation": "notEqual"
            }
          ]
        }
      },
      "name": "ifEND",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        -390,
        2640
      ]
    },
    {
      "parameters": {},
      "name": "END_WorkFlow",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        -130,
        2540
      ]
    },
    {
      "parameters": {
        "url": "=https://api.postcodes.io/postcodes/{{$json[\"postal_codes\"]}}",
        "options": {
          "ignoreResponseCode": true
        }
      },
      "name": "getPOSTCODE_API_A",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -230,
        2820
      ]
    },
    {
      "parameters": {
        "url": "=https://api.ideal-postcodes.co.uk/v1/postcodes/{{$json[\"result\"][\"postcode\"].replace(/ /g,'')}}?api_key=iddqd",
        "options": {
          "ignoreResponseCode": true
        }
      },
      "name": "getPOSTCODE_API_B",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        10,
        2820
      ]
    },
    {
      "parameters": {
        "functionCode": "const listCodes = []\nIF $node[\"getPOSTCODE_API_A\"].json[\"status\"] == 200\n  //listCodes receive list of codes\n  listCodes = $node[\"getPOSTCODE_API_A\"].json[\"result\"][\"codes\"].map(c => c.codes)\n\n//listCodes remove duplicates\n  listCodes = [...new Set(listCodes)]\n  listCodes = uniqueStr.sort()\n  \n  \nreturn ([\n{json:\n  {\n    \"new_postcode\":            $node[\"getPOSTCODE_API_B\"].json[\"result\"][\"0\"][\"postcode\"],\n    \"new_dependant_locality\":  $node[\"getPOSTCODE_API_B\"].json[\"result\"][\"0\"][\"dependant_locality\"],\n    \"new_line_1\":              $node[\"getPOSTCODE_API_B\"].json[\"result\"][\"0\"][\"line_1\"],\n    \"new_line_2\":              $node[\"getPOSTCODE_API_B\"].json[\"result\"][\"0\"][\"line_2\"],\n    \"new_listCodes\": listCodes,\n  }\n}}]);\n\nelse return error\n\n\n"
      },
      "name": "MergeHTTP",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        210,
        2820
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Postal Codes1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Postal Codes1": {
      "main": [
        [
          {
            "node": "Split Out Postal Codes1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out Postal Codes1": {
      "main": [
        [
          {
            "node": "SplitInBatchesPOSTCODE",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatchesPOSTCODE": {
      "main": [
        [
          {
            "node": "ifEND",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ifEND": {
      "main": [
        [
          {
            "node": "END_WorkFlow",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "getPOSTCODE_API_A",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "getPOSTCODE_API_A": {
      "main": [
        [
          {
            "node": "getPOSTCODE_API_B",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "getPOSTCODE_API_B": {
      "main": [
        [
          {
            "node": "MergeHTTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MergeHTTP": {
      "main": [
        [
          {
            "node": "SplitInBatchesPOSTCODE",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @tuliovargas, from taking a quick look into this, your Function node MergeHTTP doesn’t seem to contain valid JavaScript.

For example, I am not familiar with the IF syntax used. Also the .map() method belongs to the Array class but runs against codes in $node["getPOSTCODE_API_A"].json["result"]["codes"].map(c => c.codes) (which seemed to be an Object rather than an array). So from looking at the example you have provided it’s unfortunately not clear to me what exactly you want to achieve here or which purpose the loop serves.

That said, the $node variable is available in Functions and you can use it to access results from different nodes. You could, for example verify this by simply running code like this in your Function node printing the first item:

console.log(JSON.stringify($node['getPOSTCODE_API_A'].json));
console.log(JSON.stringify($node['getPOSTCODE_API_B'].json));

You can then see the items for both nodes referenced above in your browser console:

In general, using console.log() is a good and simple way of just looking at the data of your Function (or Function Item) nodes, so my advice would be to use this a lot (especially when discovering unknown methods I tend to just console.log() my stuff immediately before and after to see what exactly a method might do).

I hope this gives you some pointers to get started. Let us know if you have any further queries.