# Going in circles with error message

**URL:** https://discourse.nodered.org/t/going-in-circles-with-error-message/85060
**Category:** General
**Created:** [31 January 2024 08:33 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060 "2024-01-31T08:33:18Z")
**Posts on this page:** 10
**Page:** 1

<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: [31 January 2024 08:33 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/1 "2024-01-31T08:33:18Z")

</div>

Today's dumb question:

This code:

```auto
[{"id":"267a24d08e923993","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":3320,"y":2170,"wires":[["577228be11288c55"]]},{"id":"577228be11288c55","type":"function","z":"0918ee609bf69fc7","name":"function 37","func":"node.warn(msg.payload.foo);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3510,"y":2170,"wires":[[]]}]

```

I get `undefined`.....  
Clear, simple and easy to understand why.

So _the train is derailed_ in this bit of code:

```auto
let todays_rain = flow.get("rain") || 0;
node.warn("Today's rain is " + todays_rain);

// If `reset` message received.
if (msg.reset == "reset") {
    //
    // OLD CODE BELOW REPLACED 2023 12 27
    //
    //msg.rain_now = Math.round((flow.get("rain") * 10) / 10);
    //node.status({ text: "Rain = " + todays_rain + " counter = " + msg.count });
    //
    msg.rain_now = 0;
    node.status({text:"RESET"});
    flow.set("total_rain",0);
    return msg;
}

node.warn(" ******");
node.warn(msg.payload.rain);
node.warn(" ******");
///

```

I see the first line of ` ***** `  
but then this error:  
I get:

```auto
TypeError: Cannot read properties of undefined (reading 'rain')

```

Why am I not getting `undefined` as I do on the other machine?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [31 January 2024 08:38 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/2 "2024-01-31T08:38:51Z")

</div>

Because of your `msg` does not have `payload` from which to read the `rain`

---

<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: [31 January 2024 08:40 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/3 "2024-01-31T08:40:51Z")

</div>

Yes, but.....

(Sorry)

This bit of code is supposed to _get around_ that problem.

```auto
if (msg.payload.rain == undefined) 
{
    node.warn("no rain detected in message");
    msg.payload.rain = 0;
}
let rain_now = msg.payload.rain;
node.warn("at this point rain = " + rain_now);

```

It doesn't work.

(I put those lines in to see exactly from where the error was coming.)

Just to put it in better perspective:

```auto
let todays_rain = flow.get("rain") || 0;
node.warn("Today's rain is " + todays_rain);

// If `reset` message received.
if (msg.reset == "reset") {
    //
    // OLD CODE BELOW REPLACED 2023 12 27
    //
    //msg.rain_now = Math.round((flow.get("rain") * 10) / 10);
    //node.status({ text: "Rain = " + todays_rain + " counter = " + msg.count });
    //
    msg.rain_now = 0;
    node.status({text:"RESET"});
    flow.set("total_rain",0);
    return msg;
}

if (msg.payload.rain == undefined) 
{
    node.warn("no rain detected in message");
    msg.payload.rain = 0;
}
let rain_now = msg.payload.rain;
node.warn("at this point rain = " + rain_now);

```

But it just fails.

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [31 January 2024 08:46 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/4 "2024-01-31T08:46:07Z")

</div>

Do this:

```auto
node.warn('this is msg. do I have payload? :'+JSON.stringify(msg))
if (msg.payload.rain == undefined) 
{
    node.warn("no rain detected in message");
    msg.payload.rain = 0;
}

```

---

<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: [31 January 2024 08:53 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/5 "2024-01-31T08:53:24Z")

</div>

Confused.

 ![Screenshot from 2024-01-31 19-48-21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/2727502f73077962e153640788a7d330e776a06d.png)

But if I go to my _base line_ slightly tweaked, it works.

```auto
[{"id":"267a24d08e923993","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":3320,"y":2170,"wires":[["577228be11288c55"]]},{"id":"577228be11288c55","type":"function","z":"0918ee609bf69fc7","name":"function 37","func":"node.warn(msg.payload.rain);\n\nif (msg.payload.rain == undefined)\n{\n //\n node.warn(\"undefind\")\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":3510,"y":2170,"wires":[[]]}]

```

 ![Screenshot from 2024-01-31 19-52-10](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08b1d883a5aa3f32dd969e97ea6863d5e9b70e05.png)

So to me it does _error_ but continues.

In the other (bigger one) it ~~fails~~ errors and stops.

😕

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [31 January 2024 08:57 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/6 "2024-01-31T08:57:19Z")

</div>

The msg does not have payload.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c5ab856342b4e59bf81e1e45f034c7ad11903c8.png)

It has only properties `_msgid`, `rain_now`, `topic` and `count`

Here it fails -  
If there is no `payload` you can't ask `rain` from it

```auto
if (msg.payload.rain == undefined) 

```

---

<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: [31 January 2024 08:58 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/7 "2024-01-31T08:58:14Z")

</div>

Thanks...

I seem to be getting worse at seeing these things of late.

_Standby while I check it._

Yeah, stupidity is still happening my end.

 ![Screenshot from 2024-01-31 20-04-15](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf575a425cb2901356f30de85de81fb169cef34e.png)

And sorry I'm not using the code you posted.

So you see the correct messages (green) but the red ones are not working.

This part:

```auto
if (msg.payload.rain == undefined)
{
    node.warn("no rain detected in message");
    msg.payload.rain = 0;
    node.warn("Set payload.rain to 0");
    node.warn(msg.payload.rain);
}

```

check it has the _structure_.  
warn me (works.)  
set `msg.payload.rain = 0` (That is setting it, or do I have to make the structure first?)  
then in the next line is shows as `undefined`.

Sorry..

Did external testing on my bigger machine.  
Doesn't work on it either with a more simple example.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [31 January 2024 09:17 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/8 "2024-01-31T09:17:35Z")

</div>

Andrew, as your picture shows, `payload` is a string with `"test"`.

Therefore `payload` is a string and CAN NOT have a property named `rain`

If you want to access properties of payload BUT your flows are sending other random crap into the function, you should first [type check](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) the `payload` IS _something_ AND it is an object - before accessing its properties.

e.g.

```auto
if (msg.payload && typeof msg.payload === "object") {
    if(msg.payload.rain == undefined) {
        ....
    }
} else {
    node.error("msg.payload is either null or NOT an object", msg)
    return null
}

```

---

<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: [31 January 2024 09:28 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/9 "2024-01-31T09:28:07Z")

</div>

Yeah, ok.

That "test" was just to establish a `msg.payload` as it was pointed out that the `payload` didn't exist.

(Yes, this is only me testing - alas)

So in the `inject` node, I simply stuck `test` as the payload.  
But that only seems to have complicated the confusion.

(I took a step back and rewrote some stuff)

I made sure that the messages (now) going in HAVE `msg.payload.rain` set.

It is now working/looking better.

This part has been hard to test for a long time as _we_ haven't been getting much rain.  
Now we are and weird things are happening.

_Probably because of other stuff_

And I was trying to TEST it.  
Which opened this can of worms.

So now I've tidied up the input messages structure so now things may work better.

Thanks.  
(I'd better stop it here as I've now fixed up the input structure.)

---

<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: [14 February 2024 09:28 UTC](https://discourse.nodered.org/t/going-in-circles-with-error-message/85060/10 "2024-02-14T09:28:39Z")

</div>

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