Msg Not Defined, Yet Can't Define it?

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.


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.

1 Like

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"

1 Like

I just use:

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

Rather than these:

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.

1 Like

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

I'm not! :slight_smile:

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

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 :joy:

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.


That's good to know, thanks!

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