I am going through my NR code trying to clear the Linter error messages. I get this error with all of some similarly coded Nodes, they all do what is expected of them (control MQTT Outputs). Could someone explain to me why this error is displayed and how to get rid of it (other than ignore). Or do I need to ignore as it is a correct message because Linter cannot trap the error correctly?
Tried that and get: [msg] Expected a 'break' statement before 'case'.
I agree, you would think that the return at the very end of the code should work, but doesn't, or at least, for the Linter.
A long time ago, I got into the habit of enclosing each cases code in braces which forces JavaScript to know that they are code blocks. Doesn't usually make much difference but occasionally will catch structural errors.
Ah, I took the breaks out. I misunderstood what you were saying, and your solution looks good!!
@TotallyInformation I tend to put more Brackets in than most, it helps my brain, even with the counting up and down of left and right brackets. I think I will adopt this also.
break means jump to the end. They were redundant originally because there was a return before them so the execution never got to the break.
You can simplify your code further by using
let msg1 = {topic: Sonoff1}
let msg2 = {topic: Sonoff2}
node.status({ text: msg.boilerDivV });
switch (msg.boilerDivV) {
case 0: //HW ON (B)
msg1.payload = "OFF" //White
msg2.payload: "OFF" //Grey
global.set('boilerDivV', 0);
break;
Also the definitions of Sonoff1 and 2 should be const. Then the interpreter is free to make optimisations based on that knowledge, and it will stop you accidentally reassigning a value to it.
Next 'project' with NR is to change let to const as I come across them. These were written a while ago before const was the new let in the NR Forum.(Where I get most of my JS training from!)
Do you mean var to let or const? This is something that the Node-RED core codebase also needs to do!
Though moving let to const where the variable shouldn't be updated is also sensible. Also noting that objects and arrays can be const if all you want to do is change an entry rather than replace the whole thing.
Nope, it means that the named variable is a "constant" - sort-of. Actually means that you cannot completely replace the content of the named variable. That you can change the properties of a const object or array is one of the many strange parts of JavaScript.