# Msg Not Defined, Yet Can't Define it?

**URL:** https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396
**Category:** General
**Created:** [23 November 2024 21:43 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396 "2024-11-23T21:43:52Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)
#### Post date: [23 November 2024 21:43 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/1 "2024-11-23T21:43:52Z")

</div>

I've got several flows and I've just started cobbling together a dashboard.

On my dashboard flow that I use to populate the dashboard, I have a stand alone function that I use to call a global context GasTankStatus, so that I can put the status of the tank (empty or not empty) on my dashboard. I know the Global Context is there and I have the right path.

But when I use the function to try to get it and then send it along to a Dashboard Text module, I'm being told msg is undefined.  
Since nothing is calling this function with a message, I figure I should just declare a msg in the ON START tab and send it that way. But when I try, it tells me MSG is already defined. But if I don't declare it there and have it send, the debug tells me it's undefined.

I imagine this function might only fire once since it's not receiving a message (and I don't know if it fires every 5 seconds or not, so it may not achieve what I am trying to do) but I'd like to know why I can't get it to send anything to the text module.

 ![odd](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bbefd26631775f07c0fb161a727fb28bc5e12b55.jpeg)  
 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e8c48ebd40dafc234b34ccd1603262ea1bc22c2.jpeg)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 November 2024 22:00 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/2 "2024-11-23T22:00:20Z")

</div>

Copy/paste here the full text of the function so we can see what you are doing. Copy/paste please, not screenshot.

Also it is better not to use `var`, use `const` or `let` instead. Google to find out why.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [23 November 2024 22:06 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/3 "2024-11-23T22:06:21Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bde09b4a1373035bfa37583626efcf382d741975.png)

Your code `var msg = ""`  
is attempting to declare msg as a new variable, but msg already exists, hence the error.

Something like `var newmsg = ""` would be allowed.  
But note that if you did that, the next line `"newmsg.payload = "EMPTY"` would give an error because you just declared newmsg as a string and now you are trying to use it like an object.

I don't know exactly what your function is intended to do, but it is in the nature of nodes that they make changes to the msg object, so you may be able to get away with not declaring a variable but just setting `msg.payload = "EMPTY"`

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [23 November 2024 22:33 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/4 "2024-11-23T22:33:42Z")

</div>

I just use:

```auto
msg = {"payload": "success"};

```

Rather than these:

```auto
var msg = {"payload": "success"};
let msg = {"payload": "success"};
const msg = {"payload": "success"};

```

If I want to reuse the `msg` variable name. `msg` cannot be a string because no message object will be sent.

---

<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 November 2024 23:12 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/5 "2024-11-23T23:12:14Z")

</div>

I can't believe you're still using the old ACE editor.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [23 November 2024 23:24 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/6 "2024-11-23T23:24:20Z")

</div>

I'm not! 🙂

---

<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: [24 November 2024 00:12 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/7 "2024-11-24T00:12:57Z")

</div>

Ah haha. I seen your screenshot and assumed (what is it they say about assume 🤔)

I now realise the OP is still using ACE - meaning it's a really old version of node-red or they have an old settings file or worse, choose to use ace 😂

---

<div class="post-metadata">

### Author: ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)
#### Post date: [24 November 2024 18:22 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/8 "2024-11-24T18:22:02Z")

</div>

That's what I would have thought as well. But when I just tried msg.payload = "EMPTY" it would then result in that debug error of msg not defined. But if I tried to define it in there as a var then it said it was already there. So it was a Catch 22.

Instead of having the function floating by itself (nothing feeding into it), I just tied it into an existing node and did it that way. Now it works.

 ![node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7cc68b67b4d688d171909fba0643fbd4f9e200e9.jpeg)  
 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d8f50dcae1d1e7bb53cd8c909ab93f8645accff.jpeg)

---

<div class="post-metadata">

### Author: ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)
#### Post date: [24 November 2024 18:23 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/9 "2024-11-24T18:23:01Z")

</div>

That's good to know, thanks!

---

<div class="post-metadata">

### Author: ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)
#### Post date: [24 November 2024 18:23 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/10 "2024-11-24T18:23:50Z")

</div>

I'm using Node Red 3.1.10 from a Victron install.

---

<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: [22 February 2025 18:24 UTC](https://discourse.nodered.org/t/msg-not-defined-yet-cant-define-it/93396/11 "2025-02-22T18:24:25Z")

</div>

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