Read and test the value of a property inside an object that is inside another object

Hi, maybe a stupid question but I don't know how to ask Google about it, thanks for help.

I have this object= msg = {payload:{condition:true}} (I hope to had written it correctly)
I want to test the condition, but this instruction doesn't work:
if (msg.payload.condition===undefined) {}

Node red returns "TypeError: Cannot read property 'condition' of undefined"
Seems I can't concatenate objects simply by a "."

How can I test quickly the condition? Thank you!

I figured out myself that this syntax "msg.payload.condition" is correct. And exactly my problem is to test if there is in the incoming object this msg.payload.condition proprety declared, I have an error because it doesn't come, so how to?

Shorlty, seems I have an error because in the input msg object there isn't payload object, maybe it's because of that?

EDIT: yes it is, if I output from the previous node a msg = {value:1, payload:{}} there is no more error.
So how can I overcome the missing of this payload object to check the condition?
Thank you

That means that msg.payload is also not defined. In fact it is safer to use typeof rather than directly comparing against undefined so you could do
if (typeof msg.payload === 'undefined' || typeof msg.payload.condition === 'undefined')

2 Likes

This works because JavaScript "short circuits" or statements; when the first argument ( typeof msg.payload === 'undefined') is true it does not proceed to check the second one (typeof msg.payload.condition === 'undefined'), thereby avoiding an error should msg.payload be undefined.

2 Likes

thank you a lot!!! :slight_smile:

Tip tip...
Read the section on Working with messages

The fantastic documentation describes a method of copying the path of a nested object from the debug pane (instead of hand typing)

Seriously, 5 mins reading, you won't regret.

2 Likes

Thank you I read it, iteresting, but I love more write code instead of using modules to reach the goal, so for me functions are the solution :slight_smile:

For who may have the same problem I edited the topic to be more clear, and I report her quickly the solution.
Suppose you have an object msg wich contain another object payload, and inside of it a property called condition and you want to test it, if you are sure that the object payload and property condition should also incoming in the input object msg then it' enough to test it if (msg.payload.condition===true) {var condition=true;} else {var condition=false;} for example.
But if you are not sure that the object payload is incoming, to avoid Node Red errors and lose all the flow of that message you first have to check if object payload exist:
if (typeof msg.payload !== 'undefined')
and then you can test the condition
if (typeof msg.payload !== 'undefined') {if ( typeof msg.payload.condition !== 'undefined') {if msg.payload.condition===true {var condition=true;} else {var condition=false;}}}
I'm not really sure about it's need to check also typeof msg.payload.condition !== 'undefined' but to avoid any possible problem probably is better to

Please go back to post 4 in this topic (Read and test the value of a property inside an object that is inside another object) again and click the link in that message, study what happens and come back to flatten your structure :slight_smile:

1 Like