ERROR: Cannot read property 'value' of undefined

I have 1,500 items I split into batches of 1, then have a function that should take a value from each item, compare it to an array of values, then return a match if the same value in the array exists and a different value if it doesn’t exist.

Here’s a part of the code that I can’t get to work

let ownerToCheck;

if ($node["Split"].json['properties']['hubspot_owner_id']['value']) {
  ownerToCheck = 88656408;
} else {
  ownerToCheck = $node["Split"].json['properties']['hubspot_owner_id']['value'];
}

It works normally when the item has the property “hubspot_owner_id”, and throws Cannot read property ‘value’ of undefined if the property doesn’t exist. I’d like for it to assign the value 88656408 to the ownerToCheck variable if the property doesn’t exist, otherwise I’d like for it to compare the retrieved property value with my array (that part works.)

What am I missing? Cheers!

Hey @alazanski, funny enough I was just discussing a very similar case with @Jon.

When there is no hubspot_owner_id object, you couldn’t check for a value property of said object. So you’d need to adjust your check slightly. Assuming properties always exist you could for example do this:

if ($node["Split"].json['properties']['hubspot_owner_id'] && $node["Split"].json['properties']['hubspot_owner_id']['value'])

This would first check if hubspot_owner_id is truthy and then check if value is also truthy.

However, I am not sure if I fully grasp the logic here. It seems you’re first checking if this property has a truthy value. If so, you’re not using this value but instead set ownerToCheck to 88656408. If it doesn’t have a truthy value, you seem to set it the respective falsy value which might fail.

So I am wondering if you originally meant to do the opposite here? Like so:

let ownerToCheck;

if ($node["Split"].json['properties']['hubspot_owner_id'] && $node["Split"].json['properties']['hubspot_owner_id']['value']) {
  ownerToCheck = $node["Split"].json['properties']['hubspot_owner_id']['value'];
} else {
  ownerToCheck = 88656408;
}

Give me a shout if you have any questions on the above!

1 Like

The latter is exactly right. Thank you @MutedJam! :grin:

1 Like

Awesome, glad to hear! Many thanks for confirming :slight_smile: