# \`function\` node not showing errors in edit mode

**URL:** https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853
**Category:** General
**Created:** [28 December 2022 23:04 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853 "2022-12-28T23:04:12Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [28 December 2022 23:04 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/1 "2022-12-28T23:04:12Z")

</div>

(I seem to be a glutton for punishment.)

Ok, there is a difference with NR versions between machines. I'll cop that.

But this just confuses me.

I have 3 main machines that are usually online.  
I am not good at writing code. But I do seem to get by.  
I have a node that was working by virtue it hadn't failed to date.  
But recent events caused it to fail, and so I set about fixing it.  
I did one machine and it worked.  
Ok, so I copied the code from within the `function` node and pasted it in the other machine.  
All good, `DEPLOYED` and all was good.  
(I forgot the third machine also had this node and today it bit me.)  
I exported the code and spent 10 minutes wondering why when I `pasted` the code into the new machine, the LINE COUNT was different. (partly because of how I write code)  
Finally I realised _it_ reformatted the code. Yeah, ok, such is life.  
But when I close that I get the `red triangle` - error.  
I see red marks on the left of the window and scroll to them.  
But rather than seeing an error indication I just see _spelling corrections_ on variable names I declared.  
They work on the original machine, so I will let that slide.  
But no CLEAR indication of where the error is when I paste it in the new `function` node.

Source NR version:  
2.2.2  
Destination NR version:  
3.0.2  
(this is maybe why I want to get all machines on the same NR version.)  
I se the `midnight` theme and not sure if I use the _extended_ editor on both or not.  
It has good and bad things in each case.

## The chase:

(as in: cutting to the chase)  
It's a _Dog's breakfast_ - I know.  
260 lines of code. (There about)

Sorry - honestly. I accept I make mistakes and need to _put the work in_ to learn from them.  
But when I am not seeing any error indication it makes it a bit difficult.  
I may be using `var` rather than `let`.  
I am still not remembering which is which.  
But believe me: My code skills HAVE improved from 5 years ago when I first started.

If _you_ could indulge me looking at it, here it is.

```auto
// set up to wipe all stuff.
if (msg.payload == "WIPE") {
    context.set('m1', 0);
    context.set('m2', 0);
    context.set('m3', 0);
    context.set('mstate', 0);
    context.set('ustate', 0);
    // node.warn("WIPED");
    return;
}

if (msg.payload == "DEBUG") {
    context.set("debug", msg.onoff);
    if (msg.onoff == 0) {
        node.status({});
        // return;
    } else {
        node.status({ fill: "blue", text: "DEBUG ON" });
        // return;
    }
    return;
}

//Set Bit
function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}

//Clear Bit
function clearBit(number, bitPosition) {
    const mask = ~(1 << bitPosition);
    return number & mask;
}
/////////////////////////////////

var msg1 = {};
var msg2 = {};
//machine1 = "TelePi";
//machine2 = "TimePi";
//machine3 = "BedPi";
var machine1 = "TimePi";
var machine2 = "BedPi";
var machine3 = "TelePi";

//node.warn("Look for this " + msg.payload.Who);

var machine = msg.payload.Who;
//var link = msg.link;
var mstate = msg.payload.Modem;
var ustate = msg.payload.UpLink
var modem;
var uplink;
var value;
var m1 = context.get("m1") || 0;
var m2 = context.get("m2") || 0;
var m3 = context.get("m3") || 0;
var debug = context.get("debug") || 0;

if (debug == 1) {
    node.warn(" ************************");
    node.warn("Machine is " + machine);
    //node.warn("link is " + link);
    node.warn("Modem state is " + mstate);
    node.warn("Uplink state is " + ustate);
}

//node.warn("mstate " + mstate);

if (machine == machine1) {
    // Machine 1
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 1");
    }
    // node.warn("Machine 1");
    context.set("m1", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 0);
        context.set("mstate", newval);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 0);
            context.set("mstate", newval);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 0);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 0);
            context.set("ustate", newval);
        }
}

if (machine == machine2) {
    // Machine 2
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 2");
    }
    // node.warn("Machine 2");
    context.set("m2", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 1);
        context.set("mstate", newval);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 1);
            context.set("mstate", newval);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 1);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 1);
            context.set("ustate", newval);
        }
}

if (machine == machine3) {
    // Machine 3
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 3");
    }
    // node.warn("Machine 3");
    context.set("m3", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 2);
        context.set("mstate", newval);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 2);
            context.set("mstate", newval);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 2);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 2);
            context.set("ustate", newval);
        }
}

m1 = context.get("m1") || 0;
m2 = context.get("m2") || 0;
m3 = context.get("m3") || 0;

modem = context.get("mstate") || 0;
uplink = context.get("ustate") || 0;
msg2.payload = { modem, uplink, m1, m2, m3 };

if (debug == 1) {
    node.warn("--------------");
    node.warn("Modem value " + modem);
    node.warn("Uplink value " + uplink);
    node.warn("Counters " + m1 + " " + m2 + " " + m3);
    node.warn("--------------");
}

if (m1 == 1 && m2 == 1 && m3 == 1) {
    //
    // node.warn("All set");
    context.set("m1", 0);
    context.set("m2", 0);
    context.set("m3", 0);
    context.set("mstate", 0);
    context.set("ustate", 0);
    // modem = context.get("modem") || 0;
    // uplink = context.get("uplink") || 0;
    // Now send the message

}

if (m1 == 1 || m2 == 1 || m3 == 1) {
    //
    if (debug == 1) {
        //
        node.warn("Values being sent");
    }
    if (modem === 0) {
        uplink = "UNKNOWN";
        msg = { "payload": '<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg.colour = "red";
    } else

        if (modem > 3) {
            //node.status({fill: "green",text:machine3});
            msg = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>', "colour": "lime" };
            //msg.msg.colour = "lime";
        } else

            if (modem > 2) {
                //node.status({fill: "yellow",text:machine2});
                msg = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg.colour = "green";
            } else

                if (modem > 1) // should be `> 1`
                {
                    //node.status({fill: "red",text:machine1});
                    msg = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg.colour = "springgreen";
                }

    if (uplink === 0) {
        msg1 = { "payload": '<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg1.colour = "red";
    } else

        if (uplink > 3) {
            msg1 = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "lime" };
            //msg1.colour = "lime";
        } else

            if (uplink > 2) {
                msg1 = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg1.colour = "green";
            } else

                if (uplink > 1) // should be `> 1`
                {
                    msg1 = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg1.colour = "springgreen";
                } else

                    if (uplink == "UNKNOWN") {
                        msg1 = { "payload": '<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>', "colour": "black" };
                        //msg.colour = "black";
                    }

    // msg2.payload = {"Modem":modem,"Uplink":uplink};
    return [msg, msg1, msg2];

}

if (debug == 1) {
    //
    node.warn("NO MESSAGE");
}

return [null, null, msg2];

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [28 December 2022 23:09 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/2 "2022-12-28T23:09:14Z")

</div>

(Ok, I could EDIT the post, but...)

This is a screen shot to show the difference between the two machines.

_Good_ machine.

 ![Screenshot from 2022-12-29 10-05-17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9a093ca88d4e349ceb6026b98a6a2f4c0aa1961.png)

_Bad_ machine.

 ![Screenshot from 2022-12-29 10-05-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87b73accd35a5c02a31d85fb00f1f36c8214185c.png)

Edit screen

 ![Screenshot from 2022-12-29 10-07-37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0b293aa25267f5b387409c053a473b98b1937df.png)

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [28 December 2022 23:13 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/3 "2022-12-28T23:13:18Z")

</div>

I copied the function into the Node-red editor. It did show a few errors, all of them because, unlike **value** , **newval** is used without being declared

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [28 December 2022 23:15 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/4 "2022-12-28T23:15:17Z")

</div>

Thank you very much.

(Do you know _Mr Murphy_?)  
After posting it - as it typical with me - I noticed that.

(And should I use `let` or `var`?)

Has the way errors are shown changed between `2.x` and `3.x`?  
(Yeah, I should read the manual/update notes more/better.)

Again... Sorry and thanks.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [28 December 2022 23:23 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/5 "2022-12-28T23:23:20Z")

</div>

> [@Trying\_to\_learn](#):
>
> Has the way errors are shown changed between `2.x` and `3.x`?

Not sure when it was introduced but the default code editor was changed to Monaco fairly recently. It draws squiggly red lines under errors. When you hover the cursor over them it sticks a socking great popup in front of your face to obscure your view.

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8e7ad921324ae7398890a47487e5feb7d9e40b3.jpeg)

> [@Trying\_to\_learn](#):
>
> should I use `let` or `var`?

No idea why but the advice is to use let in preference to var.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [28 December 2022 23:29 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/6 "2022-12-28T23:29:58Z")

</div>

Again: Thanks.

Seems rocks are not as receptive to new information as brains.

I'll change the `var` to `let`. That isn't too difficult.

I saw that but for some reason dismissed it as _Spell checking_.  
I guess it happens when you get too ..... overwhelmed with different programs and their warnings.

(Not to drag this on too much and sort of fearing the _salt_ - but I'm kind asking for it)

I know there is no right way to write code.  
But is the _structure_ I have there any good?  
I don't know what I really expect in the reply, but I want to check I am not re-enforcing any more bad habits.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [29 December 2022 00:08 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/7 "2022-12-29T00:08:42Z")

</div>

There are as many ways to write code as there are to write poetry.

A good style is one where you can go back to it after 6 months and work out what it does. Yours seems well commented, spaced out and indented.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [29 December 2022 00:11 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/8 "2022-12-29T00:11:48Z")

</div>

Thanks.

As shallow as my reply may sound, it is at least saying I am not making any bad habits.  
That helps me know that I am not making things worse for myself than they need to be.  
😉

---

<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: [12 January 2023 00:12 UTC](https://discourse.nodered.org/t/function-node-not-showing-errors-in-edit-mode/72853/9 "2023-01-12T00:12:45Z")

</div>

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