Problems accessing an object

I am trying to get the data contained in the events in this object:


If I copy the path of the first object (0: object) it gives me: area.info.events[0]

But when I run the function:

msg.payload = msg.area.info.events.map(event => {
    return {
        start: event.start,
        end: event.end,
        note: event.note
    };
});

return msg;

I get "TypeError: Cannot read properties of undefined (reading 'info')"
The path looks correct to me. What am I doing wrong?

Use the "copy path" tools node-red provides to ensure correct path.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi


Additionally, use node.warn in your function e.g. node.warn({"msg.area": msg.area}) just before you access it.


Lastly, this is easier with the split and join nodes :slight_smile:

Thanks but I copied the path “ If I copy the path of the first object (0: object) it gives me: area.info.events[0]”

That confirms that message contained .area.info.events and it was an array THIS TIME, but as you did see the below error at some point _

_ it suggests a previous msg came through WITHOUT .area resulting in a type error (that is literally what the error says - couldnt read info from undefined i.e. msg.area was undefined & why I said to add node.warn({"msg.area": msg.area}) just before you access it.

If you think there are places a msg could come through that sometimes does NOT have the area property, and that is OK, then you will need to guard against it.

e.g... add a switch node before the function that checks msg.area is type of object OR in your function:

if (!Array.isArray(msg.area?.info?.events)) {
   node.error("msg.area.info.events is NOT an array!", msg) // throw catchable error
   return null // halt the flow
}
// original code below
// ...
// ...
return msg