Code node not working for JS func

Describe the problem/error/question

HI team,

Find the email value from brevoAccountEmail where brevoOwner is true and assign that brevoAccountEmail to brevoPrimaryEmail .

If hubSpotAccountEmail is present, assign hubSpotAccountEmail to ownerEmail ; otherwise, assign brevoPrimaryEmail to ownerEmail .

const brevoOwner = $("Merge").item.json["ownerShip"];
const brevoAccountEmail = $("Merge").item.json["brevoUserEmails"];
const hubSpotAccountEmail = $("Merge").item.json["hubSpotUserEmails"];

let brevoPrimaryEmail;
let ownerEmail;

// Find the brevoPrimaryEmail where brevoOwner is true
for (let i = 0; i < brevoAccountEmail.length; i++) {
    if (brevoOwner[i] === true) {
        brevoPrimaryEmail = brevoAccountEmail[i];
        break;
    }
}

// Assign ownerEmail based on the presence of hubSpotAccountEmail
if (hubSpotAccountEmail && hubSpotAccountEmail.length > 0) {
    ownerEmail = brevoAccountEmail;
} else {
    ownerEmail = brevoPrimaryEmail;
}

return {ownerEmail}

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.234.1
  • Database (default: SQLite): postgresql
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: MAC
1 Like

The “cannot read properties of undefined (reading ‘length’)” can confuse some, but typically means the object itself is undefined.

That suggest either brevoAccountEmail or hubSpotAccountEmail itself is undefined.

Try console log or return those values right after they are captured, you may have spelled something wrong or there may be more nesting in your json that is required on the variables to get that attribute.

You can also submit your entire flow JSON in a code block (by clicking </>) or using triple backticks, which can help bring people more context:
```
flow code
```
Do not worry, pinned data and anything related to auth is removed.

[
  {
    "brevoUserEmails": "[email protected]",
    "ownerShip": false,
    "hubSpotId": "479737671",
    "hubSpotUserEmails": "[email protected]"
  },
  {
    "brevoUserEmails": "[email protected]",
    "ownerShip": true
  },
  {
    "hubSpotId": "596903640",
    "hubSpotUserEmails": "[email protected]"
  },
  {
    "hubSpotId": "372393829",
    "hubSpotUserEmails": "[email protected]"
  },
  {}
]```

Hey @Gouravdev,

Looking at your code I am not sure why you are setting the const at the top instead but if you look at your input data only the first 2 items have a brevoUserEmails field so when it checks items 3, 4 and 5 where it is undefined it is throwing an error.

I would recommend checking the item exists first before trying to use it.

Yes, they are undefine as I didn’t get the values from the previous node.
I am working on the possible solutions.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.