# Reference error: msg is not defined

**URL:** <https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299>\
**Category:** General\
**Tags:** function-node\
**Created:** [23 June 2022 19:16 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299 "2022-06-23T19:16:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![omardmf](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@omardmf](https://discourse.nodered.org/u/omardmf)\
**Post date:** [23 June 2022 19:16 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299/1 "2022-06-23T19:16:56Z")

</div>

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

 ![Screenshot (633)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8d30f5a002ff90fc2e83c485c8b7295232a2cffb.png)

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [23 June 2022 19:21 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299/2 "2022-06-23T19:21:16Z")

</div>

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.

---

<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:** [23 June 2022 19:21 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299/3 "2022-06-23T19:21:23Z")

</div>

> [@omardmf](#):
>
> receiving error message the msg is not defined

> [@omardmf](#):
>
> on start function.

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)

---

<div class="post-metadata">

**Author:** ![omardmf](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@omardmf](https://discourse.nodered.org/u/omardmf)\
**Post date:** [23 June 2022 19:32 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299/4 "2022-06-23T19:32:27Z")

</div>

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 ??

 ![Screenshot (634)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/2012e4f2e09ebb858a57fe69ff9981bee4950293.png)

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [24 June 2022 00:05 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299/5 "2022-06-24T00:05:22Z")

</div>

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

---

<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:** [23 August 2022 00:05 UTC](https://discourse.nodered.org/t/reference-error-msg-is-not-defined/64299/6 "2022-08-23T00:05:24Z")

</div>

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