Another weird happening with `msg` and `null`

I am really having fun with rogue messages and strange things happening in my flows.

(my thread on subflows for buttons for instance)

So I'm confused with what I am seeing and to maybe save me a lot of typing here is a picture which I think clearly shows what I see as the problem.

And before anyone mentions it:
I have tried with no topic. SAME.

Why?

If a message isn't sent: how can OUT be created?

Just because the payload is null does not mean there is no message.

Obviously as can be seen by your debug there is indeed a message sent and the payload has been sent to Null - remember a message is a javascript object so it still has other properties such as topic etc

Craig

A bit of background on how all this happened:

A function node code - example:

if (x = 3)
{
    payload = "a"
}
else
{
    payload = "b"
}
msg.payload = payload
return msg

That is fine.

But if you make it:

if (x = 3)
{
    payload = "a"
}
else
{
    payload = null
}
msg.payload = payload
return msg

That's a whole other story.

And it is different to:
return null

Here is an example flow that shows this problem with what happens in the function node and the return stuff.

[{"id":"a8d589a97ea51bd6","type":"inject","z":"6cf51e98651df17b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2150,"y":3960,"wires":[["4934260b228560e0"]]},{"id":"4934260b228560e0","type":"function","z":"6cf51e98651df17b","name":"function 4","func":"if (msg.payload == \"A\")\n{\n    msg.payload = \"ONE\"\n}\nelse\n{\n    msg.payload = null\n}\nlet msg1 = {}\nmsg1.payload = null\nreturn [msg,msg1]","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2320,"y":3960,"wires":[["2a2a5d4f64c61a24"],["397d9434427bd94e"]]},{"id":"2a2a5d4f64c61a24","type":"change","z":"6cf51e98651df17b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Hello","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2520,"y":3940,"wires":[["b3584d6abc3f725b"]]},{"id":"b3584d6abc3f725b","type":"debug","z":"6cf51e98651df17b","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2670,"y":3940,"wires":[]},{"id":"ddc48bb67e382bd8","type":"debug","z":"6cf51e98651df17b","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2670,"y":3980,"wires":[]},{"id":"397d9434427bd94e","type":"change","z":"6cf51e98651df17b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Hello","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2520,"y":3980,"wires":[["ddc48bb67e382bd8"]]},{"id":"3008b7d591a739e7","type":"function","z":"6cf51e98651df17b","name":"function 5","func":"if (msg.payload == \"A\")\n{\n    msg.payload = \"ONE\"\n}\nelse\n{\n    msg.payload = null\n}\nreturn [msg,null]","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2320,"y":4040,"wires":[["d3acbad355dbc6d2"],["c3321f12e6d7e7d0"]]},{"id":"1c03674fbcc6a8a0","type":"inject","z":"6cf51e98651df17b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2150,"y":4040,"wires":[["3008b7d591a739e7"]]},{"id":"d3acbad355dbc6d2","type":"change","z":"6cf51e98651df17b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Hello","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2520,"y":4020,"wires":[["2f2da2919cf7c2b5"]]},{"id":"c3321f12e6d7e7d0","type":"change","z":"6cf51e98651df17b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Hello","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2520,"y":4060,"wires":[["d10e0fb321c17e60"]]},{"id":"2f2da2919cf7c2b5","type":"debug","z":"6cf51e98651df17b","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2670,"y":4020,"wires":[]},{"id":"d10e0fb321c17e60","type":"debug","z":"6cf51e98651df17b","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2670,"y":4060,"wires":[]}]

That also shows this problem happening.

(Screen shot for the sake of it.)

Compare the code in function 4 and function 5.

Yes, and I did say that even if I deleted the topic it still happened.

yeah its not the fact that you delete the topic its the fact the the msg is an object with a number of standard inclusions (like msg.id for example)

Key Considerations

  • Type Behavior : Although null is a primitive value, typeof null returns "object" due to a historical bug in JavaScript.
  • Equality : null is loosely equal (== ) to undefined but strictly not equal (=== ).
  • Arithmetic : In numerical operations, null converts to 0 (e.g., 2 + null results in 2 ), whereas undefined results in NaN .
  • Object.assign : You cannot use null as the target object for Object.assign() because it is not an object; attempting to do so will throw an error.

Yeah, thanks.

I have kinda worked that out myself ITMT.

But thanks for confirming it.

Now I am have the fun job of replacing all the places I set msg = {} and so I can leave the return [msg,msg1] and put a return [null,.msg1] in place of the msg = {}

Which is kinda messy. But if that is the only way to do it, I guess it has to be done.

If you want to test against a value you need to use ==.

Yes, but I just knocked up that code to explain the situation.

However, in light of all this new information I am slightly more intelligent hat I was before.

Just change msg = {} to msg = null

I'll try to remember this for next time.

I have lost track of what has happened.

I think I put a test in and if a condition is met, I replace the return line that returns the message with null,msg1