# Node shows code error but only AFTER I look at it

**URL:** <https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832>\
**Category:** General\
**Created:** [24 August 2022 06:04 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832 "2022-08-24T06:04:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2022 06:04 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/1 "2022-08-24T06:04:24Z")

</div>

Although there are other _problems_ in the mix, they aren't of concern at this stage.  
Yes, I know this is probably a recipe for disaster, but please indulge me.

This node code is from within a `subflow` I made (a long time ago) and it works, as in: it does what I wanted it to at time of making it.

I have since realised there is a problem with it and so I want to modify it.

I open the `function` node and then the fun begins.

I have the newer editor (not the default one) and it supposedly helps with syntax, etc.

So _status quo_ the `function` node isn't complaining and it does things correctly.  
I open it and this is the code.  
Even if I don't edit and click the `OK` button: the node shows the dreaded `red triangle` (error) on it.

Code:

```auto
msg1 = {};
msg2 = {};
msg3 = {};
msg.payload = "CLEAR";
msg1 = {payload:"Green"};
msg2 = {payload:"Green"};
msg3 = {payload:"Black"};
return [msg,msg1,msg2,msg3];

```

Sorry, but I am not seeming to see the supposed elephant to why the node says there is an error here.

All I want to do is change the last line to:  
`return [null,msg1,msg2,msg3]`

Cropped screen shots showing what is happening.

 ![Screenshot from 2022-08-24 16-05-08](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/566e74fdff16cae50d03e57e68be0ac93400a956.png)

 ![Screenshot from 2022-08-24 16-05-13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/0008ba0e7179e199e1b0920de28879aae55735ce.png)

 ![Screenshot from 2022-08-24 16-05-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/291979a027c69bb7e6af5f92524b1d47f3a25041.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2022 06:11 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/2 "2022-08-24T06:11:54Z")

</div>

# Ah!

I need it to be:

```auto
var msg1 = {};
var msg2 = {};
var msg3 = {};

```

Yes?

---

<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:** [24 August 2022 06:28 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/3 "2022-08-24T06:28:33Z")

</div>

`let` is better than `var`. Basically, don't use `var`. In this case it doesn't make any difference, but it is better to get into good habits.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2022 06:30 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/4 "2022-08-24T06:30:25Z")

</div>

Thanks.

I am not sure how/why I didn't realise that sooner.

And yes, it is better to learn good habits rather than bad ones.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [24 August 2022 10:16 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/5 "2022-08-24T10:16:39Z")

</div>

> [@Trying\_to\_learn](#):
>
> ```auto
> var msg1 = {};
> var msg2 = {};
> var msg3 = {};
> 
> ```

Is the initial object creation necessary, as it's overwritten a few lines later?  
Try -

```auto
msg.payload = "CLEAR";
let msg1 = { payload: "Green" };
let msg2 = { payload: "Green" };
let msg3 = { payload: "Black" };
return [msg, msg1, msg2, msg3];

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2022 10:23 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/6 "2022-08-24T10:23:16Z")

</div>

I think that I did it that way as another _habit_.

Define all the variables to be used.  
Assign their values in the next stage.

Yes, the way you did it is nicer, but I fear that I am not good enough to confidently do it that way yet.

In this example it is _obvious_ what needs to be done, but when I am writing bigger sets of code I an trying to establish the structure so I define all the variables at the start then lower down I assign values to them.

That is better when the value being assigned can also be variable.  
Again: This is probably a poor example, but I'm torn which way to go.

But appreciate you mentioning it.  
🙂

---

<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 August 2022 12:11 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/7 "2022-08-24T12:11:54Z")

</div>

> [@Trying\_to\_learn](#):
>
> I need it to be:
> 
> ```auto
> var msg1 = {};
> var msg2 = {};
> var msg3 = {};
> 
> ```
> 
> Yes?

In this case, `const` is the right choice.

Either ...

```auto
msg.payload = "CLEAR";
const msg1 = { payload: "Green" };
const msg2 = { payload: "Green" };
const msg3 = { payload: "Black" };
return [msg, msg1, msg2, msg3];

```

Or this ...

```auto
msg.payload = "CLEAR"
const msg1 = { }
const msg2 = { }
const msg3 = { }

msg1.payload = "Green"
msg2.payload = "Green"
msg3.payload = "Black"

return [msg, msg1, msg2, msg3];

```

... will work

* * *

RE `const`, `let`, `var`...  
My rule of thumb is: "Always use `const`, unless you cannot, then use `let`"  
There are technical reasons why but lets just say, follow the rule & avoid the technicalities.

---

<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:** [24 August 2022 19:43 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/8 "2022-08-24T19:43:56Z")

</div>

Personally, I don't like to use `const` on an object, where the contents of the object are later changed. I know that it is valid javascript, as a variable to which an object is assigned is effectively a pointer to the object, so saying `const msg1 = {}` is saying that `msg1` is a constant pointer to an object, the object itself is not constant, so it is ok to assign a value to `msg1.payload`. I know that this can be confusing, as the expectation is that using `const` would be to create a constant object, so later assigning a value to a property of the object would not be allowed. Therefore, for objects, I always use `let`, just to avoid confusion.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 August 2022 21:53 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/9 "2022-08-24T21:53:13Z")

</div>

Colin, Steve:

Thanks both.

I know - as I am sure you are aware - I've sort of been down this road before.  
I am still not 100% clear on the mechanics of what `const` `let` and `var`.  
I've been shown the documentation and looked at the _magic words_ but that is where things fail for me.

I am not by any means a programmer and am amazed by what I've done.  
I'm not sure if it is my stubbornness or I actually am _good_ at it. (Learning?)

At this stage I am trying to establish a _structure_ on how I write things that then will become constant (no pun intended) when I write code.

Alas I missed a lot of learning to code as from about the late 90's to a few years back when I came here and joined the group in Node-Red I had not done any real programming.  
Ok, a small bit in arduino, but that is even worse than my JS ability as it was done only a few times for jobs that needed to be done there and then.

JS is ongoing and I fear I am re-enforcing bad habits.

So I ask now and then to check what the more universally accepted ways are.  
Probably just as confusing and _standard_ as standards can be. 😉

---

<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:** [7 September 2022 21:53 UTC](https://discourse.nodered.org/t/node-shows-code-error-but-only-after-i-look-at-it/66832/10 "2022-09-07T21:53:44Z")

</div>

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