How check undefined msg in a function node?

Hi again everyone, this time I'm going crazy with the function node.
To understand which message arrived (each has a different name and arrives at different times) I tried various ways but seems they are wrong:

if (!msg.hasOwnProperty ('payload [0].r_id'))
if (msg.payload [0].r_id != undefined)
if (typeof msg.payload[0].r_id != "undefined")

In both cases the function gives me the error: TypeError: Cannot read property 'r_id' of undefined

How can I check if an item is not declared?

Thanks in advance

It is saying that it can't find property r_id as payload[0] isn't defined, so you need to check at that level. Eg. if (msg.payload[0] != undefined)

1 Like

Thank you @dceejay , I will try this :grinning_face_with_smiling_eyes:

I tried your solution, the problem is that some times msg.payload [0] is defined but the r_id property is not, in that case the code is executed even if it shouldn't. How can I also check r_id?

Did you try nesting the tests?

 if (msg.payload[0] != undefined) {
   if (msg.payload [0].r_id != undefined) {
      do something
   }
}
1 Like

@zenofmud yes, i tried but the error is coming when msg.payload[0] is undefined and not when have a value. I don't find an all in one solution.

To be clearer I enter the full code:

if (msg.payload[0] != "undefined"){

   if (msg.payload[0].r_id > 0 && msg.payload[0].r_id != "undefined"){

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].r_nome;

            let tempid = msg.payload[i].r_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg1.options = formatobject;

        node.send([msg1, null, null, null, null, null, null]);

   }

}

You have to test whether it is undefined before you test for >0. Swap the two round in the test.

1 Like

Ok I try thank you

It's undefined not "undefined"
if (msg.payload[0] != undefined && msg.payload[0].r_id != undefined && msg.payload[0].r_id > 0){

1 Like

You are true it's a refuse when I try typeof method, I've try @Colin and @E1cid solution but the error still remain

so what is the incoming msg to the function, when you get the type error?

I try this code and delete msg.payload with change node, the error now is: TypeError: Cannot read property '0' of undefined

if (msg.payload != undefined){

    if (msg.payload[0] != undefined) {

        if (msg.payload[0].r_id != undefined){

            let formatobject = [{ "Nessuno": 0 }];

            for (let i = 0; i < msg.payload.length; i++) {

                let tempnome = msg.payload[i].r_nome;

                let tempid = msg.payload[i].r_id;

                let tempobject = {};

                tempobject[tempnome] = tempid;

                formatobject.push(tempobject);

            }

            msg1.options = formatobject;

            node.send([msg1, null, null, null, null, null, null]);

        }

   }

}

Before the modify msg.payload is 1 (number) and the error was TypeError: Cannot read property r_id of undefined

Perhaps your payload does not exist at all or isnā€™t an array.

And by the way you should check ā€œundefinedā€ with fixed type msg.payload !== undefined

So it isnā€™t a array! To check this

if (typeof msg.payload === ā€˜arrayā€™)
1 Like

Yes payload not exist, and I get this error, another time payload exist and is an array but not have r_id property so if i check only payload the code run when don't need.
I'll try typeof thank you

I try your solution but the editor give me this error: This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"array"' have no overlap

If you want to be certain...


if(Array.isArray(msg.payload) && msg.payload.length && typeof msg.payload[0] === "object" && typeof msg.payload[0].r_id === "number") {
  // msg.payload[0].r_id is definitely present & is definitely a number - now you can compare it to a number
} 

1 Like

Thank you @Steve-Mcl , i try this but with msg,payload deleted I give TypeError: Cannot read property '0' of undefined
and with msg.payload set to 1 the error still TypeError: Cannot read property r_id of undefined

If you want I can post entire project but it's quite big... the problem is coming in the tab Notifiche
when I select Centrale termica from Reparto dropdown control_room_alpha.json (406,1 KB)

Then something else is wrong because the Array.isArray test should fail & shortcut to false

Are you certain that error comes from this function? Is there any code BEFORE this if test?

1 Like

only code is new msg objects:

var msg1 = {};

var msg2 = {};

var msg3 = {};

var msg4 = {};

var msg5 = {};

var msg6 = {};

var msg7 = {};

This is the full code of function node:

var msg1 = {};

var msg2 = {};

var msg3 = {};

var msg4 = {};

var msg5 = {};

var msg6 = {};

var msg7 = {};

//popolazione dei dropdown e azzeramento input

if (Array.isArray(msg.payload) && msg.payload.length && typeof msg.payload[0] === "object" && typeof msg.payload[0].r_id === "number") {

            let formatobject = [{ "Nessuno": 0 }];

            for (let i = 0; i < msg.payload.length; i++) {

                let tempnome = msg.payload[i].r_nome;

                let tempid = msg.payload[i].r_id;

                let tempobject = {};

                tempobject[tempnome] = tempid;

                formatobject.push(tempobject);

            }

            msg1.options = formatobject;

            node.send([msg1, null, null, null, null, null, null]);

           

}

if (!msg.hasOwnProperty('payload[0].d_id')) {

    if (msg.payload[0].d_id > 0) {

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].d_nome;

            let tempid = msg.payload[i].d_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg2.options = formatobject;

        node.send([null, msg2, null, null, null, null, null]);

    }

}

if (!msg.hasOwnProperty('payload[0].io_id')) {

    if (msg.payload[0].io_id > 0) {

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].io_nome;

            let tempid = msg.payload[i].io_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg3.options = formatobject;

        node.send([null, null, msg3, null, null, null, null]);

    }

}

if (!msg.hasOwnProperty('payload[0].u_id')) {

    if (msg.payload[0].u_id > 0) {

        let formatobject = [{"Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].u_nome;

            let tempid = msg.payload[i].u_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg4.options = formatobject;

        node.send([null, null, null, msg4, null, null, null]);

    }

}

msg5.payload = "";

node.send([null, null, null, null, msg5, null, null]);

if (!msg.hasOwnProperty('payload[0].g_id')) {

    if (msg.payload[0].g_id > 0) {

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].g_nome;

            let tempid = msg.payload[i].g_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg6.options = formatobject;

        node.send([null, null, null, null, null, msg6, null]);

    }

}

msg7.payload = 0;

node.send([null, null, null, null, null, null, msg7]);

//abilitazione e disabilitazione dropdown in base alle scelte

if (msg.reparto > 0) {

    msg2.enabled = true;

    node.send([null, msg2, null, null, null, null, null]);

}

else{

    msg2.enabled = false;

    node.send([null, msg2, null, null, null, null, null]);

}

if (msg.dispositivo > 0) {

    msg3.enabled = true;

    node.send([null, null, msg3, null, null, null, null]);

}

else {

    msg3.enabled = false;

    node.send([null, null, msg3, null, null, null, null]);

}

if (msg.utente > 0) {

    msg6.enabled = false;

    node.send([null, null, null, null, null, msg6, null]);

}

else {

    msg6.enabled = true;

    node.send([null, null, null, null, null, msg6, null]);

}

if (msg.gruppo > 0) {

    msg4.enabled = false;

    node.send([null, null, null, msg4, null, null, null]);

}

else {

    msg4.enabled = true;

    node.send([null, null, null, msg4, null, null, null]);

}

return;