# Checking for string, nested states throws undefined error

**URL:** <https://discourse.nodered.org/t/checking-for-string-nested-states-throws-undefined-error/68841>\
**Category:** General\
**Tags:** function-node\
**Created:** [11 October 2022 07:28 UTC](https://discourse.nodered.org/t/checking-for-string-nested-states-throws-undefined-error/68841 "2022-10-11T07:28:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/akrea/32/68902_2.png) [@akrea](https://discourse.nodered.org/u/akrea)\
**Post date:** [11 October 2022 07:28 UTC](https://discourse.nodered.org/t/checking-for-string-nested-states-throws-undefined-error/68841/1 "2022-10-11T07:28:13Z")

</div>

Hi

Here es what I am trying to do:

- I have a nested statemachine (smxstate node).
- Depending on the state, I need to enable disable buttons in the dashboard

The (reduced) code I have is:

```auto
if (msg.payload.state == "idle"){ 
    return [ 
        {enabled: true}, // Button 1
        {enabled: true}, // Button 2
        {enabled: true}, // Button 3
        ...
    ]; 
} else if (msg.payload.state.nested1 == "state_xy") {
    return [
        {enabled: true}, // Button 1
        {enabled: false}, // Button 2
        {enabled: false}, // Button 3
        ...
    ];
} else {
    return [
        {enabled: false}, //Button 1
        {enabled: false}, //Button 2
        {enabled: false}, //Button 3
        ...
    ]; 
}

```

The code works as intended. But whenever the nested criteria is `false` i get `TypeError: Cannot read property 'nested1' of undefined`. I don't know if the same would happen for non-nested criteria as the only non-nested one is the `"idle"` one.

As I said the whole code is working but i have a ton of messages in the debugging-message window, which makes it hard to debung non-working stuff.

Thank you for your time and effort helping the community and noobs like me.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 October 2022 08:17 UTC](https://discourse.nodered.org/t/checking-for-string-nested-states-throws-undefined-error/68841/2 "2022-10-11T08:17:19Z")

</div>

For starters, it is much better to get into the habit of using exact comparitors for if statements with `===` rather than `==`.

Secondly, while you've not shared any example data, I'm guessing you have a msg coming through that does not have a msg.payload.state at all. Maybe msg.payload is a string rather than an object? You need to exclude that from your if's.

---

<div class="post-metadata">

**Author:** ![akrea](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/akrea/32/68902_2.png) [@akrea](https://discourse.nodered.org/u/akrea)\
**Post date:** [11 October 2022 11:13 UTC](https://discourse.nodered.org/t/checking-for-string-nested-states-throws-undefined-error/68841/3 "2022-10-11T11:13:21Z")

</div>

> [@TotallyInformation](#):
>
> For starters, it is much better to get into the habit of using exact comparitors for if statements with `===` rather than `==`

Thank you for the good practice input!

> [@TotallyInformation](#):
>
> I'm guessing you have a msg coming through that does not have a msg.payload.state at all.

Bullseye!! I have the state machine firing off a second message, which I momentarily forgot.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 October 2022 11:13 UTC](https://discourse.nodered.org/t/checking-for-string-nested-states-throws-undefined-error/68841/4 "2022-10-25T11:13:50Z")

</div>

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