Error in getting part of an object

I have this simple function to read the specific lines of my interest in a message and assign the topic.

msg.payload = msg.payload.commonAnnotations.description;
msg.topic = msg.payload.commonAnnotations.summary;
return msg

I have copied both paths from the debug window.

The msg.topic is giving error

"TypeError: Cannot read properties of undefined (reading 'summary')"

I don't understand why and how to resolve this.
The msg.payload it is fine. I can get it without errors.

Your first line replaces msg.payload.
Therefore msg.payload.commonAnnotations.summary no longer exists.

Set msg.topic first.

You can avoid that by using something like

let annotations = msg.payload.commonAnnotations
msg.payload = annotations.description;
msg.topic = annotations.summary;
return msg
1 Like

Thank you. I was blind!

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