# If statement with function node

**URL:** <https://discourse.nodered.org/t/if-statement-with-function-node/24505>\
**Category:** General\
**Created:** [9 April 2020 08:10 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505 "2020-04-09T08:10:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [9 April 2020 08:10 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/1 "2020-04-09T08:10:36Z")

</div>

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.

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b176195b8128f638a980ead76f5d977fd6884515.png)

The code inside the function node :

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/99746e352b3a8dbdf053569b8788fdfdfadb49b8.png)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 April 2020 08:22 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/2 "2020-04-09T08:22:33Z")

</div>

Please edit your post above (click the pencil icon) and format your code as [per this forum guide](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506).  
It will then be easier to read, as it highlights the code syntax.

---

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [9 April 2020 10:01 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/3 "2020-04-09T10:01:11Z")

</div>

thanks so much for the note .... done

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [9 April 2020 10:13 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/4 "2020-04-09T10:13:01Z")

</div>

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](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously)  
Johannes

---

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [9 April 2020 10:59 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/5 "2020-04-09T10:59:06Z")

</div>

@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 ???

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [9 April 2020 11:00 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/6 "2020-04-09T11:00:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [9 April 2020 11:33 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/7 "2020-04-09T11:33:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 April 2020 07:35 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/8 "2020-04-10T07:35:15Z")

</div>

Try your function node like (select 2 outputs):

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

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

```

---

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [11 April 2020 18:54 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/9 "2020-04-11T18:54:20Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [11 April 2020 19:31 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/10 "2020-04-11T19:31:08Z")

</div>

It should be something like this:

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

---

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [12 April 2020 13:06 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/11 "2020-04-12T13:06:09Z")

</div>

@ 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

---

<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:** [11 June 2020 13:06 UTC](https://discourse.nodered.org/t/if-statement-with-function-node/24505/12 "2020-06-11T13:06:13Z")

</div>

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