Node-Red 3 complains about me

Hi,
i installed Node-Red 3 on both of my RPi.
Just on one of them Node-Red complains about my functions:


for example. Every function it complains something...

on the other RPi (same code) Node-Red does´t complain anything.

I don´t found an option to enable/disable complaining.

There is a way but this is literally telling you you have "bad" code.

Declare your variables with const or let and they will be fixed.
const if it doesnt get changed, let if it does.

If you hover the red squiggle it tells you what is wrong.
If you click the light bulb it offers solutions (not always perfect, but it tries)

1 Like

Thx, I unterstand with const / let.
But this does not answere my question: why is the other Pi with NR3 not complaining? Is there a switch?

I used "var Pth =..." before, but without "var" it worked too... so I deleted all "var" to reduce my code.

Older node-red uses ACE editor. From version 3, Monaco editor is the default - and for good reason - it tells you about coding problems, it offers hints and built in help and it is generally much more upto date with modern JS. It has a whole bunch of handy snippets (e.g. start typing global or clone or status then hit the tab key to see how it both helps you AND gets it right every time).

If you really want to revert, search for the word monaco in your settings.js file and change it to ace, restart, refresh. NOTE: the ace editor will quite probably be removed altogether after NR v4 so (IMHO) I recommend you get used to it and improve your code.

PS: Monaco is the same editor used in the insanely popular code editor "VS Code" - it is good - I would recommend sticking it out.

2 Likes

And perhaps you should rename this thread to "Node-RED V3 function node tells me about poor coding - how do i switch it off" :stuck_out_tongue:

(joking)


NOTE: Just because the editor complains, does not mean you have to switch back to the depreciated ACE editor.

If you click the light bulb, you can turn off error checking for that node - but keep all the nice tooltips and auto complete and snippets and built in help!

All good :upside_down_face:
i need a strict teacher :joy:
grafik
In this code I need "let" or ?

The best way to evaluate this is...

  1. Use const always
  2. If something complains, change it to let

Another way to look at it is...

If you declare something like const myVar = 1 then later want to change it myVar = 2 then it should be a let

i.e. if there is a separate line of code with an = on the right, (e.g. myVar = somethingElse) - then use let.

PS - monaco editor usually tells you if you get it wrong :+1:

:face_with_monocle:

1: let Strom = ...
4: Strom = parseFloat

would this work with
1: const Strom = ...
4: Strom = parseFloat

in line 4 I´m changing or do I not ?


what about functions with more than 1 output:
var msg1 = {};
var msg2 = {};

let ? const ?

Yes

No

const - UNLESS you later on do msg1 = {somethingElse} or msg1 = "hello"

This would be flagged by monaco and tell you you cannot reassign a const variable


Try to remember what I said in prev post.

it tried with const and it works - I don´t unterstand this...

Can you show us the function that should fall please. Screenshot.
Does it fail when it runs?

You have to show me exactly what you wrote for me to be able to understand properly.

Basic rules as I said are const if you never change it let if you do.

The only exception to this is you can create a const object but still change its properties. That is what you are doing.

I think I got it now, I was a little bit confused, sorry.
the monaco editor helps me very well.

1 Like

what happens if I don't prescribe var,let,const ?
what does the compiler do with it ?

It defaults to var, I believe, in order to be able to correctly run legacy code.

1 Like

thx, what about Ä Ö Ü ?
grafik
the editor is marking it with red -> not allowed?

And why are some variables turquoise and other black ?
--> got it, capital letter turquoise

Although it's possible to use Unicode Character in variables names, I don't recommend it. My first language is Portuguese, which has a lot of accented characters, and I don't use them in variables. Many chances of technical problems, besides the typos; If it's already difficult without accents, imagine with them. And the colors are another help from Monaco editor, depends on the theme you are using. But in this case, the editor is not recognizing the Übers as a variable, that's why it's got a different color. Try to rename to Ubers and see the change. About themes : https://discourse.nodered.org/t/how-to-use-themes-with-monaco-editor/48310

1 Like

I believe that what happens is that the JIT assigns the variable to the current global scope. This may have unexpected side effects.

If you use var, the variable is "hoisted" to the top of your top-level current code scope which again can have confusing side effects. Though very occasionally, this can be used to simplify your code - use with caution though.

let and const are never hoisted and are defined at the current local scope. So if you define something within { .... } brackets using let or const, it is defined only for that scope and is not available outside it. This is also good for memory efficiency as it allows the garbage collector routine to recover memory more quickly.

Sidebar: Little known but occasionally useful, in JavaScript you can use { ... } on their own (not just with a function, if/then, etc) to create a new scope. A bit like creating a function and immediately calling it but without the function overheads. Variables are inherited downwards of course so you don't need function arguments in this case.

2 Likes

A good explanation here...

1 Like

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