# Help with error in function that...works!

**URL:** <https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376>\
**Category:** General\
**Tags:** javascript\
**Created:** [10 March 2023 06:06 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376 "2023-03-10T06:06:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre\_x](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@andre\_x](https://discourse.nodered.org/u/andre_x)\
**Post date:** [10 March 2023 06:06 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/1 "2023-03-10T06:06:59Z")

</div>

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

```auto
var timestamp = Date.now();
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';
    }else{
        newMsg.headers['priority'] = '2';
    }    
    return newMsg;
}

```

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

---

<div class="post-metadata">

**Author:** ![MecatronicaMADE](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@MecatronicaMADE](https://discourse.nodered.org/u/MecatronicaMADE)\
**Post date:** [10 March 2023 06:17 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/2 "2023-03-10T06:17:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [10 March 2023 06:29 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/3 "2023-03-10T06:29:18Z")

</div>

> [@andre\_x](#):
>
> ` newMsg = {};`

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 = {};`

---

<div class="post-metadata">

**Author:** ![andre\_x](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@andre\_x](https://discourse.nodered.org/u/andre_x)\
**Post date:** [10 March 2023 06:31 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/4 "2023-03-10T06:31:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [10 March 2023 06:37 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/5 "2023-03-10T06:37:31Z")

</div>

> [@andre\_x](#):
>
> I was missing a `var`

Despite you can use `var`, [you shouldn't](https://codeburst.io/javascript-var-let-or-const-which-one-should-you-use-2fd521b050fa). Use `let`!

---

<div class="post-metadata">

**Author:** ![andre\_x](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@andre\_x](https://discourse.nodered.org/u/andre_x)\
**Post date:** [10 March 2023 06:49 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/6 "2023-03-10T06:49:26Z")

</div>

I wasn't aware of it, thanks!

---

<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:** [10 March 2023 09:20 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/7 "2023-03-10T09:20:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 March 2023 09:36 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/8 "2023-03-10T09:36:00Z")

</div>

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.

> [@andre\_x](#):
>
> `var`

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.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [10 March 2023 10:09 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/9 "2023-03-10T10:09:06Z")

</div>

> [@TotallyInformation](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 March 2023 10:25 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/10 "2023-03-10T10:25:29Z")

</div>

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.

---

<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:** [24 March 2023 10:26 UTC](https://discourse.nodered.org/t/help-with-error-in-function-that-works/76376/11 "2023-03-24T10:26:00Z")

</div>

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