# Check if message.status.text exists

**URL:** <https://discourse.nodered.org/t/check-if-message-status-text-exists/74155>\
**Category:** General\
**Created:** [25 January 2023 06:17 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155 "2023-01-25T06:17:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [25 January 2023 06:17 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/1 "2023-01-25T06:17:19Z")

</div>

Hi,  
I am using a status node to check if an exec is running. If the exec is started or killed the status is known.  
However if the exec has not been started or killed, the message.status.text doesn't exist and then I get an error.  
I did try this function but it doesn't work.

```auto
if ((msg.status.text) !== undefined)
  var test = msg.status.text;
  msg.payload = "something....."

```

the error is: "TypeError: Cannot read properties of undefined (reading 'text')"

How do you detect if msg.status.text is undefined?

Thanks for helping

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 06:30 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/2 "2023-01-25T06:30:46Z")

</div>

I'm not an expert.

```auto
if ((msg.status.text) !== undefined)
  var test = msg.status.text;
  msg.payload = "something....."

```

I think you need to use `{ }` around it.  
But there again, maybe not.  
Just your formatting - to me - is confusing.

Implies you want to do this:

```auto
if ((msg.status.text) !== undefined)
{
  // not set.
  var test = msg.status.text;
  msg.payload = "something....."
}
// Now it is set.

```

Maybe:  
But it could be

```auto
if ((msg.status.text) !== undefined)
{
  // If not defined.
  var test = msg.status.text;
} // If it is defined.
msg.payload = "something....."

```

But you may need to post a bit more of the code.  
Because I am not seeing how/where `test` is used/needed.

Maybe - in retrospect:

```auto
if ((msg.status.text) !== undefined)
{
  // If not defined.
  msg.status.text = "Not defined";
} // If it is defined.
// msg.status.text is defined and can now be handled downstream from here. 

```

But I don't know exactly if this will help or not.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [25 January 2023 06:41 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/3 "2023-01-25T06:41:44Z")

</div>

> [@FeliceM](#):
>
> How do you detect if msg.status.text is undefined?

```auto
if (msg.status?.text !== undefined) {
    ...
}

```

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [25 January 2023 06:44 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/4 "2023-01-25T06:44:19Z")

</div>

Thanks for highlighting the missing braces. It was a copy past mistake.  
this is the correct one:

```auto
if ((msg.status.text) !== undefined) {
  var test = msg.status.text;
  msg.payload = "Process not running"
}

return msg;

```

the issue is that the condition "undefined" is wrong. The code still try to read the msg.status.text which doesn't exists.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 06:46 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/5 "2023-01-25T06:46:32Z")

</div>

But where is `test` used?

Can you post a bit more of the flow?

Maybe like the `exec` node and `status` node?

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [25 January 2023 06:46 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/6 "2023-01-25T06:46:37Z")

</div>

This seems to give a different result. Let me investigate it better. Thanks for now...

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [25 January 2023 07:04 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/7 "2023-01-25T07:04:25Z")

</div>

Perhaps I should have given some more details:

Your initial check stumbles over the fact that you can test for `.text` only if `.status` exists.  
Yet if `.status` is missing, it's obvious that `.text` will be `undefined` as well.

How's `?.` ([optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)) working?

> The **optional chaining (`?.`)** operator accesses an object's property or calls a function. If the object accessed or function called is [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) or [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null), it returns [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) instead of throwing an error.

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [25 January 2023 07:11 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/8 "2023-01-25T07:11:24Z")

</div>

Thanks a million for the solution and for the explanation. It is now working correctly.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 January 2023 07:41 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/9 "2023-01-25T07:41:37Z")

</div>

Good to hear!

It's nice when you get a solid answer to a question/problem.

---

<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:** [8 February 2023 07:42 UTC](https://discourse.nodered.org/t/check-if-message-status-text-exists/74155/10 "2023-02-08T07:42:19Z")

</div>

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