Function node error

Hi everyone
I’m trying to take an item from the body of an input, and convert it to all lowercase.

Here’s the JSON:
image

Here’s my function:

let final = $node["Webhook"].json["body.merchantCompanyLegalName"];
finaltext = final.toLowerCase();
items[0].json.text = finaltext;
console.log(finaltext)
return items;

And I’m getting
ERROR: Cannot read properties of undefined (reading ‘toLowerCase’) [Line 2]

TypeError: Cannot read properties of undefined (reading 'toLowerCase')
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function:2:19
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function:6:2
    at VM2 Wrapper.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/bridge.js:485:11)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/nodevm.js:426:23)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function/Function.node.js:121:30)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:645:51)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:557:68
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Problem in node ‘Function‘

Cannot read properties of undefined (reading ‘toLowerCase’) [Line 2]

Information on your n8n setup

  • n8n version: 0.197.1
  • Database you’re using (default: SQLite): SQLite
  • Running n8n with the execution process [own(default), main]: default
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker-compose (DigitalOcean)

Hey @bakkbone,

Welcome to the community :raised_hands:

Is there more to your workflow and are you using a function or function items node? As a quick test it might be worth outputting your final variable to make sure it is correct, I suspect that is the issue.

1 Like

Hi Jon :slight_smile:

Here’s the full workflow (with pinned data edited to replace with fake data)

{
  "name": "MW Init",
  "nodes": [
    {
      "parameters": {},
      "id": "990f9687-4176-4b6e-ba7c-0e837e8bf8ca",
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        175,
        235
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://base.merchantwarrior.com/partner/",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "method",
              "value": "addMerchant"
            },
            {
              "name": "partnerUUID",
              "value": "1234567890"
            },
            {
              "name": "apiKey",
              "value": "1234567890"
            },
            {
              "name": "merchantCompanyLegalName",
              "value": "={{ $json[\"body\"][\"merchantCompanyLegalName\"] }}"
            },
            {
              "name": "merchantCompanyTradingName",
              "value": "={{ $json[\"body\"][\"merchantCompanyTradingName\"] }}"
            },
            {
              "name": "merchantCompanyRegNumber",
              "value": "={{ $json[\"body\"][\"merchantCompanyRegNumber\"] }}"
            },
            {
              "name": "merchantIndustry",
              "value": "={{ $json[\"body\"][\"merchantIndustry\"] }}"
            },
            {
              "name": "merchantWebsite",
              "value": "={{ $json[\"body\"][\"merchantWebsite\"] }}"
            },
            {
              "name": "merchantCountry",
              "value": "={{ $json[\"body\"][\"merchantCountry\"] }}"
            },
            {
              "name": "merchantState",
              "value": "={{ $json[\"body\"][\"merchantState\"] }}"
            },
            {
              "name": "merchantCity",
              "value": "={{ $json[\"body\"][\"merchantCity\"] }}"
            },
            {
              "name": "merchantStreet",
              "value": "={{ $json[\"body\"][\"merchantStreet\"] }}"
            },
            {
              "name": "merchantPostcode",
              "value": "={{ $json[\"body\"][\"merchantPostcode\"] }}"
            },
            {
              "name": "merchantTimezone",
              "value": "={{ $json[\"body\"][\"merchantTimezone\"] }}"
            },
            {
              "name": "merchantContactName",
              "value": "={{ $json[\"body\"][\"merchantContactName\"] }}"
            },
            {
              "name": "merchantContactEmail",
              "value": "={{ $json[\"body\"][\"merchantContactEmail\"] }}"
            },
            {
              "name": "merchantContactPhone",
              "value": "={{ $json[\"body\"][\"merchantContactPhone\"] }}"
            },
            {
              "name": "merchantContactMobile",
              "value": "={{ $json[\"body\"][\"merchantContactMobile\"] }}"
            },
            {
              "name": "merchantBillingContactName",
              "value": "={{ $json[\"body\"][\"merchantBillingContactName\"] }}"
            },
            {
              "name": "merchantBillingContactEmail",
              "value": "={{ $json[\"body\"][\"merchantBillingContactEmail\"] }}"
            },
            {
              "name": "ddSettlementAccountName",
              "value": "={{ $json[\"body\"][\"ddSettlementAccountName\"] }}"
            },
            {
              "name": "ddSettlementAccountBSB",
              "value": "={{ $json[\"body\"][\"ddSettlementAccountBSB\"] }}"
            },
            {
              "name": "ddSettlementAccountNumber",
              "value": "={{ $json[\"body\"][\"ddSettlementAccountNumber\"] }}"
            },
            {
              "name": "ccSettlementAccountName",
              "value": "={{ $json[\"body\"][\"ccSettlementAccountName\"] }}"
            },
            {
              "name": "ccSettlementAccountBSB",
              "value": "={{ $json[\"body\"][\"ccSettlementAccountBSB\"] }}"
            },
            {
              "name": "ccSettlementAccountNumber",
              "value": "={{ $json[\"body\"][\"ccSettlementAccountNumber\"] }}"
            },
            {
              "name": "cardAcceptorName",
              "value": "={{ $json[\"body\"][\"cardAcceptorName\"] }}"
            },
            {
              "name": "merchantCategoryCode",
              "value": "={{ $json[\"body\"][\"merchantCategoryCode\"] }}"
            },
            {
              "name": "hash",
              "value": "={{ $json[\"body\"][\"hash\"] }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "MW-API-VERSION",
              "value": "2.0"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {}
          ]
        },
        "options": {}
      },
      "id": "7f8f3e71-dfc7-44bd-8126-1ae5c9d483d3",
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        660,
        400
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "mwinit",
        "options": {}
      },
      "id": "66ab2e07-5d6f-4df4-87cf-4a6def6e51bc",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -40,
        400
      ],
      "webhookId": "c6d6a872-4c2e-4d8e-8225-4faa56ce388d"
    },
    {
      "parameters": {
        "value": "=0cfab45877bc72b258e79c12998b54476348e9bbd51b4{{$node[\"Webhook\"].json[\"body\"][\"merchantCompanyLegalName\"]}}{{$node[\"Webhook\"].json[\"body\"][\"merchantCompanyRegNumber\"]}}",
        "dataPropertyName": "body.hash"
      },
      "id": "89f8ee81-2679-4bdc-b8c3-8bfca597da5c",
      "name": "Crypto",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        420,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "let final = $node[\"Webhook\"].json[\"body.merchantCompanyLegalName\"];\nfinaltext = final.toLowerCase();\nitems[0].json.text = finaltext;\nconsole.log(finaltext)\nreturn items;"
      },
      "id": "dd9b897a-cdb4-41ef-9d07-c73093c1757d",
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        180,
        400
      ]
    }
  ],
  "pinData": {
    "Webhook": [
      {
        "json": {
          "headers": {
            "host": "api.bakkbone.au",
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 X-Middleton/1",
            "content-length": "1174",
            "accept": "*/*",
            "accept-encoding": "deflate, gzip",
            "content-type": "application/json",
            "x-forwarded-for": "206.189.205.251",
            "x-forwarded-host": "api.bakkbone.au",
            "x-forwarded-proto": "https",
            "x-real-ip": "115.70.107.209"
          },
          "params": {},
          "query": {},
          "body": {
              "merchantCompanyLegalName": "LANDERS, LINDA MARIE",
              "merchantCompanyTradingName": "BAKKBONE Australia",
              "merchantCompanyRegNumber": "89407920728",
              "merchantIndustry": "Utilities",
              "merchantWebsite": "https://www.bakkbone.com.au/",
              "merchantCountry": "Australia",
              "merchantState": "Western Australia",
              "merchantCity": "Canning Vale",
              "merchantStreet": "123 Fake St",
              "merchantPostcode": "6155",
              "merchantTimezone": "Australia/Perth",
              "merchantContactName": "Scott Landers",
              "merchantContactEmail": "[email protected]",
              "merchantContactPhone": "1234567890",
              "merchantContactMobile": "1234567890",
              "merchantBillingContactName": "Scott Landers",
              "merchantBillingContactEmail": "[email protected]",
              "ddSettlementAccountName": "BAKKBONE Australia",
              "ddSettlementAccountBSB": "123456",
              "ddSettlementAccountNumber": "12345678",
              "ccSettlementAccountName": "BAKKBONE Australia",
              "ccSettlementAccountBSB": "123456",
              "ccSettlementAccountNumber": "12345678",
              "cardAcceptorName": "BAKKBONE Australia",
              "merchantCategoryCode": "4814",
              "additionalOptions": "Direct Debit",
              "accountOption": "MW All-In-One - No Annual Fee",
          }
        }
      }
    ],
    "Crypto": [
      {
        "json": {
          "headers": {
            "host": "api.bakkbone.au",
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 X-Middleton/1",
            "content-length": "1174",
            "accept": "*/*",
            "accept-encoding": "deflate, gzip",
            "content-type": "application/json",
            "x-forwarded-for": "206.189.205.251",
            "x-forwarded-host": "api.bakkbone.au",
            "x-forwarded-proto": "https",
            "x-real-ip": "115.70.107.209"
          },
          "params": {},
          "query": {},
          "body": {
            "merchantCompanyLegalName": "LANDERS, LINDA MARIE",
            "merchantCompanyTradingName": "BAKKBONE Australia",
            "merchantCompanyRegNumber": "89407920728",
            "merchantIndustry": "Utilities",
            "merchantWebsite": "https://www.bakkbone.com.au/",
            "merchantCountry": "Australia",
            "merchantState": "Western Australia",
            "merchantCity": "Canning Vale",
            "merchantStreet": "123 Fake St",
            "merchantPostcode": "6155",
            "merchantTimezone": "Australia/Perth",
            "merchantContactName": "Scott Landers",
            "merchantContactEmail": "[email protected]",
            "merchantContactPhone": "1234567890",
            "merchantContactMobile": "1234567890",
            "merchantBillingContactName": "Scott Landers",
            "merchantBillingContactEmail": "[email protected]",
            "ddSettlementAccountName": "BAKKBONE Australia",
            "ddSettlementAccountBSB": "123456",
            "ddSettlementAccountNumber": "12345678",
            "ccSettlementAccountName": "BAKKBONE Australia",
            "ccSettlementAccountBSB": "123456",
            "ccSettlementAccountNumber": "12345678",
            "cardAcceptorName": "BAKKBONE Australia",
            "merchantCategoryCode": "4814",
            "additionalOptions": "Direct Debit",
            "accountOption": "MW All-In-One - No Annual Fee",
            "hash": "1234567890sdfghjkl8uybi"
          }
        }
      }
    ]
  },
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crypto": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Crypto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 12,
  "meta": {
    "instanceId": "8816cbb0556450ed07c38872a7de2f7be3173a6b3a905f8950298674b143dd58"
  },
  "tags": []
}

I haven’t yet referenced the output of the function because I can’t get it working, but it would be replacing the {{$node[“Webhook”].json[“body.merchantCompanyLegalName”]}} in the crypto node

I’m also not familiar with JS myself so this is all new for me :confused:

Close, you just need to change $node["Webhook"].json["body.merchantCompanyLegalName"] to $node["Webhook"].json.body.merchantCompanyLegalName. You can also safeguard your operation if the field doesn’t exist for some reason, here’s a version of your workflow using the function item node (runs once per item):

Also worth reading up on property accessors in JS - Property accessors - JavaScript | MDN

3 Likes

Thanks so much!