# Function with multiple payloads (with null)

**URL:** https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867
**Category:** General
**Created:** [9 July 2022 08:19 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867 "2022-07-09T08:19:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [9 July 2022 08:19 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/1 "2022-07-09T08:19:33Z")

</div>

Hi,  
I try to improve my code daily and today I'd like to change this code:

```auto
// 1
var bc = global.get("var_bloccocar");
if (bc == 1)
{
    global.set("var_bloccocar", 0)
    msg1 = {labelcolorcar:"#A4A4A4"}
}
else
{
    msg1 = []
}

// 2
var bw = global.get("var_bloccowet");
if (bw == 0)
{
    var bw2 = global.get("var_bloccowet2")
    if (bw2 == 1)
    {
        global.set("var_bloccowet2", 0)
        msg2 = {labelcolorwet:"#A4A4A4"}
    }
    else
    {
        msg2 = []
    }
}
else
{
    msg2 = []
}
.
.
.
.
return [msg1, msg2, msg3, msg4, .................];

```

I have a lot of this function with 8/10/14 outputs and for each I have ....

```auto
else
    {
        msg ..... = []
    }

```

Is there a more "elegant" solution?

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [9 July 2022 09:33 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/2 "2022-07-09T09:33:04Z")

</div>

I would use null instead of `[]` if no msg should be sent to the corresponding output. In general I am not a friend of using function nodes in Node-Red,

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [9 July 2022 10:09 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/3 "2022-07-09T10:09:50Z")

</div>

Without knowing what your function does i can only suggest you set all outputs to null at the start then only alter the ones that fit the conditions

```auto
const output = [null,null,null,null....etc];
// 1
let bc = global.get("var_bloccocar");
if (bc == 1)
{
    global.set("var_bloccocar", 0)
    output[0] = {labelcolorcar:"#A4A4A4"}
}

// 2
let bw = global.get("var_bloccowet");
let bw2 = global.get("var_bloccowet2");
if (bw == 0 && bw2 == 1)
{
        global.set("var_bloccowet2", 0)
        output[1] = {labelcolorwet:"#A4A4A4"}

}

.
.
.
.
return output;

```

---

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [9 July 2022 10:34 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/4 "2022-07-09T10:34:16Z")

</div>

I think this is a very good solution; thank you!

You change VAR in LET, is there a reason?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [9 July 2022 10:44 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/5 "2022-07-09T10:44:45Z")

</div>

var is global scope and its better to use const and let which are block scope

> **[The Difference Between var, let and const in JavaScript and Best Practices](https://stackabuse.com/the-difference-between-var-let-and-const-in-javascript-and-best-practices/)**
>
> In this guide, learn what the difference is between the var, let and const variable declarations in JavaScript, including best practices, with examples.

---

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [9 July 2022 10:58 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/6 "2022-07-09T10:58:44Z")

</div>

I'm going to study; thank you 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: [23 July 2022 10:59 UTC](https://discourse.nodered.org/t/function-with-multiple-payloads-with-null/64867/7 "2022-07-23T10:59:04Z")

</div>

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