Going in circles with error message

Today's dumb question:

This code:

[{"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:

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:

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

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

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

Yes, but.....

(Sorry)

This bit of code is supposed to get around that problem.

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:

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.

Do this:

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;
}

Confused.

But if I go to my base line slightly tweaked, it works.

[{"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":[[]]}]

So to me it does error but continues.

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

:confused:

The msg does not have payload.
image

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

if (msg.payload.rain == undefined) 
1 Like

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.

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:

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.

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 the payload IS something AND it is an object - before accessing its properties.

e.g.

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
}
1 Like

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.)

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