Node shows code error but only AFTER I look at it

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.


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.


I need it to be:

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


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.

1 Like


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.

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

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

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.

1 Like

In this case, const is the right choice.

Either ...

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

Or this ...

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.

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.


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. :wink:

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