How to access attribute tags in javascript node

I am trying to access the value of an attribute tag from a device in a workflow using the javascript node. The workflow gets the device, all attributes and attribute tags and saves them on the payload. From within the javascript node I try to access an att tag but get an error:

FunctionNodeTypeError
Cannot read properties of undefined (reading 'attributeTags')

The code that is attempting to access the tag is:

/* attribute calc-flow is for airflow, attribute tag ductAreaCuFt holds duct csa */
var ductCsa = Number(4);
ductCsa = Number(payload.working.deviceId.attributes.calc-flow.attributeTags.ductAreaCuFt);

The payload showing in the workflow debug pane is:
Capture1

How can I acces the value ‘4’ stored in this attribute tag?

Also, what is a way a javascript node can test the payload to see if an element exists before accessing it so the entire workflow won’t error?

First, your problem lies here (emphasis added) -

payload.working.deviceId.attributes.calc-flow.attributeTags.ductAreaCuFt

Dot notation does not support hyphens in property names (along with some other special characters). Instead, you’ll have to use bracket notation for that portion of the path:

payload.working.deviceId.attributes['calc-flow'].attributeTags.ductAreaCuFt

Also, what is a way a javascript node can test the payload to see if an element exists before accessing it so the entire workflow won’t error?

There is! The Function Node supports optional chaining for safely accessing deep object properties that may not exist.

So using your example from above, a safe way to access the property would be …

const myValue = payload.working?.deviceId?.attributes?.['calc-flow']?.attributeTags?.ductAreaCuFt;

Note you need that period before the bracket notation when doing optional chaining.

Thanks, that works well.