# Nodered 3.02 functions

**URL:** <https://discourse.nodered.org/t/nodered-3-02-functions/67109>\
**Category:** General\
**Tags:** function-node\
**Created:** [30 August 2022 07:31 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109 "2022-08-30T07:31:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 07:31 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/1 "2022-08-30T07:31:00Z")

</div>

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

```auto
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"}];

```

---

<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:** [30 August 2022 07:39 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/2 "2022-08-30T07:39:43Z")

</div>

> [@pvanklink](#):
>
> Do i have to rewrite all my functions ?

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

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 07:58 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/3 "2022-08-30T07:58:27Z")

</div>

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

---

<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:** [30 August 2022 08:02 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/4 "2022-08-30T08:02:28Z")

</div>

> [@pvanklink](#):
>
> 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](https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75)

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.**

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 10:54 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/5 "2022-08-30T10:54:07Z")

</div>

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 =

```auto
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;

```

---

<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:** [30 August 2022 11:08 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/6 "2022-08-30T11:08:41Z")

</div>

> [@pvanklink](#):
>
> why cant i use the let statement in de for.... loop on lines 19 and 20: let jsontmp= and let teken =

- `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...

```auto
        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...

> [@pvanklink](#):
>
> ```auto
> let i = 0;
> for (i = 0; i < nwobj.length; i++) {
> jsontmp = jsontmp + teken + JSON.stringify(nwobj[i]);
> teken = ",";
> }
> 
> ```

declare the variable `i` inside the `for` ...

```auto
        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?

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

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 August 2022 11:34 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/7 "2022-08-30T11:34:09Z")

</div>

> [@Steve-Mcl](#):
>
> `var` should be avoided

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.

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 11:45 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/8 "2022-08-30T11:45:48Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 August 2022 11:49 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/9 "2022-08-30T11:49:45Z")

</div>

Please share your updated code

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 11:50 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/10 "2022-08-30T11:50:50Z")

</div>

```auto
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;

```

---

<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:** [30 August 2022 11:58 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/11 "2022-08-30T11:58:18Z")

</div>

Looks fine to me.

> [@pvanklink](#):
>
> i get an error with const teken=,

There is no issue with that line....

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55fc509d560fb8143fad5af46259cc78c6b0d10a.png)

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 12:02 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/12 "2022-08-30T12:02:12Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> const nwobj = msg.payload.output[msg.payload.selected]
> msg.json = JSON.stringify(nwobj);
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 12:03 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/13 "2022-08-30T12:03:16Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [30 August 2022 12:23 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/14 "2022-08-30T12:23:07Z")

</div>

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

---

<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:** [29 October 2022 12:23 UTC](https://discourse.nodered.org/t/nodered-3-02-functions/67109/15 "2022-10-29T12:23:45Z")

</div>

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