# Can I access number of outputs within function node?

**URL:** https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729
**Category:** General
**Created:** [13 January 2019 01:27 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729 "2019-01-13T01:27:39Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [13 January 2019 01:27 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/1 "2019-01-13T01:27:39Z")

</div>

My function node has 6 outputs. Is there a way to access this number from within the function node?

i.e. so I don't have to explicitly specify the number in my first line:

```
var outputcount = 6;

// Clear all backgrounds on any type of change
var outputs = [];
for (i=0; i<outputcount; i++) {
    outputs[i] = {background : ""};
}

// If the change was that a scene was set, light up that scene
if (msg.topic == "home/topliving/light/scene") {
    // we read from global memory, instead of from incoming msg
    // so scenes are remembered even when redeploying
    var currentScene = global.get("home.topliving.light.scene");
    for (i=0; i<outputcount; i++) {
        if (i == currentScene) {
            outputs[i] = { background: "#00A676" };
        }
    }
}
return outputs;
```

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [13 January 2019 09:30 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/2 "2019-01-13T09:30:08Z")

</div>

No - there is no API for that.

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [13 January 2019 13:15 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/3 "2019-01-13T13:15:47Z")

</div>

Thanks for confirming

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [13 January 2019 14:24 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/4 "2019-01-13T14:24:21Z")

</div>

> [@hazymat](#):
>
> Is there a way to access this number from within the function node?

As the number of outputs is stored in the `flow_xx.json`, you could read it from the file:

```auto
function get_outputs(node_name, callback) {
    var fs = global.get('fs');
    const flow_file = '/home/pi/.node-red/flows_rpi4.json';
    
    fs.readFile(flow_file, function (err, data) {
        if (err) {
            node.warn(err);
        } else {
            var nodes = JSON.parse(data);
            Object.keys(nodes).forEach(function(key) {
                if (nodes[key].name === node_name) {
                    callback(nodes[key].outputs);
                }
            });
        }
    });
}

get_outputs('fun_name', function(outputs) {
    msg.payload = outputs;
    node.send(msg);
});

```

Replace `fun_name` by the name of you function and `flow_rpi4.json` by your flow name.  
**Note:** fs has to be defined in the `settings.js`

```auto
[{"id":"2d3bbb37.d24584","type":"function","z":"ac4aa9f6.c24288","name":"fun_name","func":"function get_outputs(node_name, callback) {\n var fs = global.get('fs');\n const flow_file = '/home/pi/.node-red/flows_rpi4.json';\n \n fs.readFile(flow_file, function (err, data) {\n if (err) {\n node.warn(err);\n } else {\n var nodes = JSON.parse(data);\n Object.keys(nodes).forEach(function(key) {\n if (nodes[key].name === node_name) {\n callback(nodes[key].outputs);\n }\n });\n }\n });\n}\n\nget_outputs('fun_name', function(outputs) {\n msg.payload = outputs;\n node.send(msg);\n});\n","outputs":2,"noerr":0,"x":360,"y":1600,"wires":[["a2137d2d.e68aa"],[]]},{"id":"a2137d2d.e68aa","type":"debug","z":"ac4aa9f6.c24288","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":550,"y":1600,"wires":[]},{"id":"a037c85e.9e5c28","type":"inject","z":"ac4aa9f6.c24288","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1600,"wires":[["2d3bbb37.d24584"]]}]

```

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [13 January 2019 22:13 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/5 "2019-01-13T22:13:03Z")

</div>

Hi @cflurin - that’s pretty impressive! I note the function node API gives access to the flow name (node.name), so it could be further improved. Is there a way to access the name of the flow this function is within? If so - that would make for a generic code block that could be included in any function without modification!

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [14 January 2019 06:13 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/6 "2019-01-14T06:13:58Z")

</div>

Right, you can use `node.name` or `node.id` (preferred).

```auto
function get_outputs(callback) {
    const fs = global.get('fs');
    const flow_file = '/home/pi/.node-red/flows_rpi4.json';
    
    fs.readFile(flow_file, function (err, data) {
        if (err) {
            node.warn(err);
        } else {
            var nodes = JSON.parse(data);
            Object.keys(nodes).forEach(function(key) {
                // if (nodes[key].name === node.name) {
                if (nodes[key].id === node.id) {
                    callback(nodes[key].outputs);
                }
            });
        }
    });
}

get_outputs(function(outputs) {
    msg.payload = outputs;
    node.send(msg);
});

```

> [@hazymat](#):
>
> Is there a way to access the name of the flow this function is within?

No, I don't think it's possible - but if you use the node.id, the node identification is unique.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [14 January 2019 08:51 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/7 "2019-01-14T08:51:29Z")

</div>

Hey @cflurin,

I haven't tested it yet in a function node, but _ **perhaps** _ you can use something like this:

```auto
RED.nodes.eachNode(function(anotherNode) {
    if (anotherNode].id === node.id) {
         // Do something (e.g. with node.name) ...
    }
});

```

Then you don't have to parse the file, since Node-RED has already loaded everything in memory ...  
Bart

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 January 2019 09:01 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/8 "2019-01-14T09:01:37Z")

</div>

> [@BartButenaers](#):
>
> but _ **perhaps** _ you can use something like this:

No, the `RED.nodes.eachNode` api is _not_ available in the Function node.

This is a lot of work to avoid keeping a number in the Function and the Outputs value in sync... how often do you change it?!

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [14 January 2019 10:30 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/9 "2019-01-14T10:30:43Z")

</div>

> [@knolleary](#):
>
> This is a lot of work to avoid keeping a number in the Function and the Outputs value in sync

Agree! I probably won't do it for that reason, would be great if the API might support it in the future though...

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [14 January 2019 11:12 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/10 "2019-01-14T11:12:59Z")

</div>

> [@BartButenaers](#):
>
> RED.nodes.eachNode(function(anotherNode)

As @knolleary said the `RED` api doesn't work in a function. However it's available in core and contrib-nodes.  
It also works in the dsm-node. As an example see the [Flow statistics](https://github.com/cflurin/node-red-contrib-dsm/wiki/Flow-statistics).

---

<div class="post-metadata">

### Author: ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)
#### Post date: [14 January 2019 13:07 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/11 "2019-01-14T13:07:02Z")

</div>

Pretty useless cause one has to wire the outputs anyway by hand...

To do something like that just output an array -\> split -\> switch. This also follows more the msg based philosophy of node-red.

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [14 January 2019 14:03 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/12 "2019-01-14T14:03:33Z")

</div>

> [@moebius](#):
>
> one has to wire the outputs anyway by hand

I don't understand

The reason I'd find it useful is that I want to have a generic function that I can reuse about 20 times. One for each lighting zone. The number of circuits in a given lighting zone may change from time to time. So it would make life easier.

No problem really changing the number by hand, just wondered if there was a better way. Either way I've learned a lot just from this thread so thanks to all!

---

<div class="post-metadata">

### Author: ![moebius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moebius/32/2872_2.png) [@moebius](https://discourse.nodered.org/u/moebius)
#### Post date: [14 January 2019 14:55 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/13 "2019-01-14T14:55:41Z")

</div>

Changing outputs on multiple (20?) functions and rewire them doesnt mean generic for me  
and the whole approach looks pretty (over-)complicated (even if you didnt tell what exactly you want to achieve).

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [14 January 2019 16:56 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/14 "2019-01-14T16:56:05Z")

</div>

> [@moebius](#):
>
> the whole approach looks pretty (over-)complicated (even if you didnt tell what exactly you want to achieve).

I really don't understand what point you are trying to make

Was just asking if there was a built-in way to determine number of outputs; as if so, I'd use it when writing function code

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [14 January 2019 18:48 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/15 "2019-01-14T18:48:28Z")

</div>

Agreed -- this and all other properties of the `function` node's definition _should_ be available to the code inside it...

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [14 January 2019 19:44 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/16 "2019-01-14T19:44:22Z")

</div>

err - no... there are many properties of a node that would not be appropriate to expose to the inside of a function.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [14 January 2019 20:02 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/17 "2019-01-14T20:02:13Z")

</div>

Well, internal properties I agree should **not** be necessarily exposed -- I guess I was only thinking of the properties entered into the edit panel by the flow author...

... which I see now is only the Name and # of outputs. So my statement still stands ;\*)

Actually, the labels on the output ports _would_ be helpful for writing modular function code.

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [14 January 2019 20:20 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/18 "2019-01-14T20:20:48Z")

</div>

Splitting output to different branches can be done with default set of nodes in many different ways. Multiple outputs of the function node is only one of them. And function stays reusable if you create the logic by following the strategy path the Node-RED offers.  
By asking supportive system expose itself - in JavaScript land it's basically "ask it to be open for modifications" and this will be opened gate to self-destruction. Do you really want this kind of system to rely to build your stuff on top? Probably not.

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [14 January 2019 23:40 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/19 "2019-01-14T23:40:43Z")

</div>

Sorry if I’m being dim here, but I don’t understand any of the reasons people are giving that it’s a _bad_ idea to be able to determine the number of outputs from within a function node. To my untrained ear, it literally sounds like people are being superstitious about it. I cannot see how Node RED would fall apart if it allowed users to access something like the number of outputs. After all, it allows you to access other things about the function node like its name. Self destruction? Appropriate? Sorry I don’t understand this!

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 January 2019 07:15 UTC](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729/20 "2019-01-15T07:15:35Z")

</div>

Hi @hazymat, I can see no reason for not making `node.outputs` available. I will make it so.

[Next page](https://discourse.nodered.org/t/can-i-access-number-of-outputs-within-function-node/6729.md?page=2)
