Reference error: msg is not defined

i'm using mqtt to send and recieve messages on node red.
i'm receiving "Red" or "Green" messages and import them to an on start function.

var Color = msg.payload
if (Color === "Red") {
return msg;
else if (Color === "Green") {
return msg;
else return null;

the function output is a label called "Product", i want it to display "Green" or "Red" and if no message is being sent i want it to display nothing but i'm receiving error message the msg is not defined

That usually means you have no data in your message. Without more information everything from here on is a quess. Have you put a debug node on your node to see what the data is? Possibly a flow might help to see what you are doing.

there is no msg in the "on start" function code, only "on message"

The "on start" tab is for when the function node is initialised (one time only, at creation/node-red start).

The "on message" tab is ran everytime a message arrives (thats where you can use msg.xxxx).

The "on stop" tab is ran only when the function node is destroyed (deleted, node-red stopped)

When i'm not receiving "Green" or "Red" i'm receiving null " ", but it's not getting updated in the ui.
should i change the value format of the label ??

You can check for null , then if you see null you can send whatever or nothing at all.

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