# How can my custom node have dynamic number of outputs?

**URL:** https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256
**Category:** Developing Nodes
**Created:** [13 January 2020 11:00 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256 "2020-01-13T11:00:59Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 January 2020 11:00 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/1 "2020-01-13T11:00:59Z")

</div>

Hi all.

I have tried searching and looking through node-red function and switch nodes but cant pin down how I can have 1 or 2 outputs depending on a users choice in a drop-down

For example, suppose I had an option on my custom node...

- Throw errors (default)
- Send errors in msg.error
- Send errors to separate output

When a user chooses "Send errors to separate output" I'd like the node to change from having 1 output to having 2 outputs.

Any ideas?

---

<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: [13 January 2020 11:02 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/2 "2020-01-13T11:02:33Z")

</div>

Hi @Steve-Mcl

the trick is to add a property to your node's `defaults` object called `outputs`.

You can then modify `this.outputs` as you would any other node property. The editor knows to watch that property and update the node accordingly.

NIck

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 January 2020 11:31 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/3 "2020-01-13T11:31:18Z")

</div>

Perfect - thank you

---

<div class="post-metadata">

### Author: ![hklages](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hklages/32/23899_2.png) [@hklages](https://discourse.nodered.org/u/hklages)
#### Post date: [10 December 2020 18:50 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/4 "2020-12-10T18:50:29Z")

</div>

What am I doing wrong? I always get the message "Cannot use in operator ...".

```auto
RED.nodes.registerType('sonosevents-notify', {
    category: 'sonosevents',
    defaults: {
      confignode: {
        value: '',
        type: 'sonosevents-config'
      },
      outputs: 1
    },
    inputs: 0, // set the number of inputs - only 0 or 1
    //outputs: 1, // set the number of outputs - 0 to n
    color: '#AAAAAA',
    label: function() {
      return 'events';
    },
    paletteLabel: 'sonosevents'
  });

```

---

<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: [10 December 2020 18:54 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/5 "2020-12-10T18:54:57Z")

</div>

You still need it at the higher level. As the original answer said you need to add it to defaults (as well)

---

<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: [10 December 2020 19:08 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/6 "2020-12-10T19:08:02Z")

</div>

Whilst you _have_ added an `outputs` property to the `defaults` object, it isn't properly configured. You need to follow the same pattern used by the other properties in the defaults object:

```auto
defaults: {
   outputs: { value: 1 }
}

```

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [30 December 2020 20:13 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/7 "2020-12-30T20:13:36Z")

</div>

> [@knolleary](#):
>
> the trick is to add a property to your node's `defaults` object called `outputs` .

This gets me almost but not quite where I want to go. I want my node to have one or two outputs, depending on the state of a checkbox. It seems like the variable associated with the checkbox is not available until I close the edit dialog, at which point it is too late to change the `outputs` property. The second time I close the dialog, the `oneditsave` code picks up the change. There must be something I can do, but my jQuery skills are severely limited. Any suggestions?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [30 December 2020 20:27 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/8 "2020-12-30T20:27:46Z")

</div>

You should be able to set `node.outputs` in an on change event.

Supposing your check box is named `node-input-check` and "checked" means 2 outputs & unchecked means 1 output, this might be overkill but it covers all bases...

```auto
defaults: {
    name: { value: "" },
    check: { value: true },
    outputs: { value: 2 }
}

...

oneditprepare: function () {
    var node = this;
    node.outputs = $("#node-input-check").prop('checked') ? 2 : 1;
    $("#node-input-check").change(function() {
        node.outputs = this.checked ? 2 : 1;
    });            
},
oneditsave: function () {
    var node = this;
    node.outputs = $("#node-input-check").prop('checked') ? 2 : 1;
}

```

NOTES: I always try to ensure `outputs` and `check` default to matching state (e.g. if you default the check to checked then default `outputs` to 2) so that when you add a new node and defaults are applied, they match each other.

_(untested)_

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [30 December 2020 20:39 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/9 "2020-12-30T20:39:09Z")

</div>

Thanks. This looks like just what I need. I'll give it a try.

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [30 December 2020 21:31 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/10 "2020-12-30T21:31:58Z")

</div>

> [@Steve-Mcl](#):
>
> this might be overkill

Works a treat. Also seems to work without the `oneditprepare` section. Is that what you meant by "overkill"?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [30 December 2020 22:00 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/11 "2020-12-30T22:00:50Z")

</div>

yes.

the reason I added it there also was for other code below inside `oneditprepare` that was dependant.

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [30 December 2020 22:12 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/12 "2020-12-30T22:12:39Z")

</div>

Good point. Better safe than sorry. Thanks again.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [23 March 2023 14:51 UTC](https://discourse.nodered.org/t/how-can-my-custom-node-have-dynamic-number-of-outputs/20256/13 "2023-03-23T14:51:48Z")

</div>

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