If statement with function node

Hi
I have the following flow which is read mac address from the database. Then there is a function node that works to checks the mac existence of the mac by using the if statement as shown in the code below. it's run an only first statement of if and ignore the rest in spite of they are true...
What I wanted is to check each condition then return the message.

The code inside the function node :

var i;

for (i=0 ;i< msg.payload.length;i++){
    
if (msg.payload[i].mac=="2D:60" ){
   
msg.payload=msg.payload ="aldo ";

msg.payload= {employee:msg.payload ,timestamp :new Date().toString (),status :"check in"};
 return msg;

}
 if (msg.payload[i].mac=="abc" ){
    
msg.payload=msg.payload ="james ";

msg.payload= {employee:msg.payload ,timestamp :new Date().toString (),status :"check in"};
 return msg;
     
 }}

it's run only the first "if" statement as shown in the output below.
output

Please edit your post above (click the pencil icon) and format your code as per this forum guide.
It will then be easier to read, as it highlights the code syntax.

1 Like

thanks so much for the note .... done

1 Like

Your function will only ever run till it hits a return statement. If you want to send multiple messages from a function you will have to use the async methods instead of return msg; as this does what it says it returns from the function and nothing after this statement runs.
You will have to use node.send(msg) instead. Have a look at the part about asynchronous messages in the documentation:
https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously
Johannes

1 Like

@JGKK
thanks for your kind reply.

I don't have much experience with this. I have tried the node.send(msg) but nothing changed .....

Have you tried it before ???

Post your function with node.send() Please and ill have a look at it

var i;
var mac;
var myStringArray = msg.payload;

for (i=0 ;i< msg.payload.length;i++){
var result = (myStringArray[i]);
if (msg.payload[i].mac=="C0:B1" ){
    
msg.payload=msg.payload ="aldo ";

msg.payload= {employee:msg.payload ,timestamp :new Date().toString (),status :"check in"};

node.send(msg);
}

if (msg.payload[i].mac=="abc" ){
    
msg.payload=msg.payload ="james ";

msg.payload= {employee:msg.payload ,timestamp :new Date().toString (),status :"check in"};

node.send(msg);
}
 // return msg;
}

I have tried other ways like send each message to different output [msg,null] and for the second [null,msg] but the same...

Try your function node like (select 2 outputs):

m = msg.payload

t = new Date().toISOString()
o1 = null
o2 = null
s = "check in"

for(x=0;x<m.length;x++){
    
    if(m[x].mac=="C0:B1"){
        o1 = {payload:{employee:"Aldo" ,timestamp:t,status:s}}
    }  
     if(m[x].mac=="abc"){
        o2 = {payload:{employee:"James" ,timestamp:t,status:s}}
    }  
    
}
return [o1,o2]

Note that a function node can only return a single message at a time - if more are required (like in loops) use node.send.

Example flow:

[{"id":"7aef04eb.54e37c","type":"inject","z":"202708de.aa6658","name":"","topic":"","payload":"{\"arr\":[\"C0:B1\",\"abc\"]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":244,"y":240,"wires":[["ec36c06d.fd4c"]]},{"id":"ec36c06d.fd4c","type":"function","z":"202708de.aa6658","name":"","func":"m = msg.payload.arr\n\nt = new Date().toISOString()\no1 = null\no2 = null\ns = \"check in\"\n\nfor(x=0;x<m.length;x++){\n    \n    if(m[x]==\"C0:B1\"){\n        o1 = {payload:{employee:\"Aldo\" ,timestamp:t,status:s}}\n    }  \n     if(m[x]==\"abc\"){\n        o2 = {payload:{employee:\"James\" ,timestamp:t,status:s}}\n    }  \n    \n}\nreturn [o1,o2]","outputs":2,"noerr":0,"x":399,"y":240,"wires":[["7e19673f.2654e"],["4c6a2e24.1b9038"]],"l":false},{"id":"7e19673f.2654e","type":"debug","z":"202708de.aa6658","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":471,"y":216,"wires":[],"l":false},{"id":"4c6a2e24.1b9038","type":"debug","z":"202708de.aa6658","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":471,"y":264,"wires":[],"l":false}]
1 Like

@bakman2

Thanks so much for your kind reply. I am trying to work with node.send as it most fit for what I am looking for

It should be something like this:

let person;
let newmsg;
msg.payload.forEach(item => {
    if (item.mac == "C0:B1"){
        person = "aldo";
    } else if (item.mac == "abc"){
        person = "james";
    }
    newmsg = {employee: person, timestamp: new Date().toString(), status: "check in"};
    node.send({payload: newmsg});
});
node.done();
return;

this is a bit of guess work as you never showed us the original payload you put into the function.
If you have many different macs your looking for you might want to substitute the if else if for a switch case statement.
Johannes

1 Like

@ JGKK

thanks, that's great. Yes, this can do the job........
Sorry, I don't have real data yet to share it with you , its just random data I have added to test the flow ...

Thanks Once again

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