Nodered 3.02 functions

I upgraded to nodered 3.02.
Most of my functions give errors...
Do i have to rewrite all my functions ?

receivers='';

if(flow.get("receiver_1") === true)
    {receivers = global.get("receiver1")}

if(flow.get("receiver_2") === true)
    {if(flow.get("receiver_1") === true)
        {receivers= global.get("receiver1") + ',' + global.get("receiver2");}
    else
        {receivers= global.get("receiver2");}}

msg.receivers = receivers;

return[msg,{topic:"Control",payload:"queue"}];

No, they will continue to work in the runtime.

However, they will show you errors in the editor since the default level of checking was set to help users who are typically not programmers. i.e. it lets you know you have missed a const/let

In your case, you are simply missing a let (e.g. let receivers='';) will fix this one. NOTE: it is good practice to declare variables properly as it helps avoid bugs.

If you must revert to the old editor and keep your bad code, then you can switch from monaco to ace (for now - will be depreciated in the next node-red version). The setting for the code editor can be found in your settings.js file

Hi Steve, thanks!
I choose to change my bad code :slight_smile:
So is using let the most save option?
Var en const are also options, or is let more generic?

  • var should be avoided
  • const is safer
  • let when const is not possible

Good read: JavaScript ES6+: var, let, or const? | by Eric Elliott | JavaScript Scene | Medium

It also echo's my personal mantra...

Use let when you know that the value of a variable will change.
Use const for every other variable.
Do not use var.

2 Likes

I used your mantra and have a question...
why cant i use the let statement in de for.... loop on lines 19 and 20: let jsontmp= and let teken =

const sel = msg.payload.selected;
const nwvar = "Geen opties";

if (sel === nwvar) {
        const json = "<empty>"
        msg.json = json;
        msg.oms3 = sel;
        msg.payload = sel     // deze waarde hebben andere list nodes nodig om te bepalen wat aan/uit
        msg.device = sel;     //extra
        flow.set("json", json);
        flow.set("device", sel); //sel
}
else {
        const nwobj = msg.payload.output[msg.payload.selected]
        let teken = "";
        let jsontmp = "";
        let i = 0;
        for (i = 0; i < nwobj.length; i++) {
                jsontmp = jsontmp + teken + JSON.stringify(nwobj[i]);
                teken = ",";
        }
        let json = "[" + jsontmp + "]";

        msg.json = json;
        msg.oms3 = sel;
        msg.payload = sel     // deze waarde hebben andere list nodes nodig om te bepalen wat aan/uit
        msg.device = sel;     //extra
        flow.set("json", json);
        flow.set("device", sel); //sel
}

return msg;
  • jsontmp is used AFTER the for loop so must be declared outside of the for loop
  • teken could be declared inside the loop but it would be recreated every loop so you would need to do it differently...
        let jsontmp = "";
        let i = 0;
        for (i = 0; i < nwobj.length; i++) {
                const teken = index === 0 ? "" : ","
                jsontmp = jsontmp + teken + JSON.stringify(nwobj[i]);
                teken = ",";
        }
        let json = "[" + jsontmp + "]";

PS...

declare the variable i inside the for ...

        for (let i = 0; i < nwobj.length; i++) {
                jsontmp = jsontmp + teken + JSON.stringify(nwobj[i]);
                teken = ",";
        }


Lastly, it looks like you are creating an array in string format from an existing array? Why not simply stringify the array?

        const nwobj = msg.payload.output[msg.payload.selected]
        msg.json =  JSON.stringify(nwobj);

To clarify, there ARE some occasions where var is valid and might be used. Just not too many. It is helpful to understand what var actually does as it is more complex than it seems. Avoid in general use but worth learning for that odd occasion where it makes your code simpler.

i get an error with const teken=,
it does not now index,
do i have to use i instead ?

and i think that the line teken = "," must be removed ?

Please share your updated code

const sel = msg.payload.selected;
const nwvar = "Geen opties";

if (sel === nwvar) {
        const json = "<empty>"
        msg.json = json;
        msg.oms3 = sel;
        msg.payload = sel     // deze waarde hebben andere list nodes nodig om te bepalen wat aan/uit
        msg.device = sel;     //extra
        flow.set("json", json);
        flow.set("device", sel); //sel
}
else {
        const nwobj = msg.payload.output[msg.payload.selected]
        //let teken = "";
        let jsontmp = "";
        for (let i = 0; i < nwobj.length; i++) {
                const teken = i === 0 ? "" : ","
                jsontmp = jsontmp + teken + JSON.stringify(nwobj[i]);
        }
        let json = "[" + jsontmp + "]";

        msg.json = json;
        msg.oms3 = sel;
        msg.payload = sel     // deze waarde hebben andere list nodes nodig om te bepalen wat aan/uit
        msg.device = sel;     //extra
        flow.set("json", json);
        flow.set("device", sel); //sel
}

return msg;

Looks fine to me.

There is no issue with that line....

image

agree, but the original had index instead off i....

This is the result, think i can remove the // lines

const sel = msg.payload.selected;
const nwvar = "Geen opties";

if (sel === nwvar) {
        const json = "<empty>"
        msg.json = json;
        msg.oms3 = sel;
        msg.payload = sel     // deze waarde hebben andere list nodes nodig om te bepalen wat aan/uit
        msg.device = sel;     //extra
        flow.set("json", json);
        flow.set("device", sel); //sel
}
else {
        const nwobj = msg.payload.output[msg.payload.selected]
        //let teken = "";
        let jsontmp = "";
        for (let i = 0; i < nwobj.length; i++) {
                const teken = i === 0 ? "" : ","
                jsontmp = jsontmp + teken + JSON.stringify(nwobj[i]);
        }
//        let json = "[" + jsontmp + "]";
//        let json = JSON.stringify(jsontmp);

        msg.json = JSON.stringify(jsontmp);
        msg.oms3 = sel;
        msg.payload = sel     // deze waarde hebben andere list nodes nodig om te bepalen wat aan/uit
        msg.device = sel;     //extra
        flow.set("json", JSON.stringify(jsontmp));
        flow.set("device", sel); //sel
}

return msg;

Thanks to you all !
It works great, and learning again...