How can my custom node have dynamic number of outputs?

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?

1 Like

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

1 Like

Perfect - thank you

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

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'
  });

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

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:

defaults: {
   outputs: { value: 1 }
}
1 Like

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?

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

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)

2 Likes

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

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

yes.

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

Good point. Better safe than sorry. Thanks again.

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