Help with error in function!

When I deploy Node Red says that there is an error in one of my function, this one:

var timestamp =;
var disable_notification_until = global.get("disable_notification_until");
if (disable_notification_until <= timestamp){
    newMsg = {};
    newMsg.payload = msg.payload.message;
    newMsg.headers = {};
    newMsg.headers['X-Title'] = msg.payload.title;
    newMsg.headers['topic'] = msg.topic_ntfy;
    newMsg.headers['tags'] = 'houses';
    if (msg.payload.bip == true){
        newMsg.headers['priority'] = '4';
        newMsg.headers['priority'] = '2';
    return newMsg;

I can't find the error and the function works as expected!
Where is the error?

Hi @andre_x Where does it tell you that there is an error?
In a debug node? can you share a screenshot of the error?
Or in the node itself? if so, hovering over the red triangle should show the type of error, you can share it.
it would also help if you shared your flow.

Looks (to me) like this is a (global) variable definition missing let or var - that is indicated as an error.

Try to make this
let newMsg = {};

1 Like

Ahahaha, damned!!! You're right, I was missing a var in front of newMsg = {};!

Despite you can use var, you shouldn't. Use let!

I wasn't aware of it, thanks!

There would have been a little red line under the error in the code window, but that can be difficult to see. However, it also shows a red marker in the scroll bar on the right hand side where the error is, so if you have a problem then look for that and you should be able to see where the problem is.

Strictly, you should use const there since it shouldn't ever be replaced. You can still add to a const object or update its properties - same for a const array.

In the case of a small function node like this, the use of var isn't much of an issue. In larger code however, the use of var is inefficient and can lead to subtle errors since var's are "hoisted" which is to say that the javascript engine does not define the vars at the point you write them but rather at the begining of your code. So it is hard for the engine to free the memory again. Let/const localises the definitions which not only means that accidental reuse of the same variable name is less likely to cause problems but also that the memory can be free'd sooner.

1 Like

Let's agree, that const is an alternative here. It's technically correct as well for the code as is - yet as well creating a constraint. let is the swiss army knife to declare variables in Javascript; doesn't solve all use cases - but most.

Apologies for extending the side-conversation - I'll shut up after this - promise!

The reason for using const is to avoid more subtle errors and force them out early. Since trying to replace a const variable will raise an error. They can be a really useful thing in more complex code and personally, I train myself to try to use best practice in the easy stuff so that the more difficult bits aren't so painful.

But of course, in short snippets of code that most people will use in a function node, this really won't make a tremendous amount of difference.


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