`function` node not showing errors in edit mode

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

// 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];

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

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

Good machine.

Bad machine.

Edit screen

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

1 Like

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.

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.

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

1 Like

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.

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.

1 Like

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.
:wink:

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