How do I iterate over all nodes in the editor (from within an input element event handler)?
(I want to find a node with a name matching the current input)
Just to not leave this question unanswered:
answers for this question and similar ones (dealing with the internals of Node-RED) may be found in some postings written by Nick O'Leary for the Node-RED forum
The following is just a copy from there:
Everything that follows only applies in the editor. This is no equivalent in the runtime.
There's no direct way to find anything by its 'name' - names are optional fields and can be left blank. They are also not unique. Under the covers you should be using the id field to reference other nodes.
let node = RED.nodes.node(id);
To get any nodes whose name property is set to a particular value:
let results = [];
RED.nodes.eachNode(function(node) {
if (node.name === "HELLO") {
results.push(node);
}
});
(If you dig around the code, you'll see RED.nodes.filterNodes
exists - that currently only filters on z
and type
rather than any other property... but it would be simply enough to update to handle any property... we have never needed it)
The above works for regular flow nodes. To do the same for Config nodes or Flows, you'd do the same but with eachConfig()
and eachWorkspace()
respectively.
let node = RED.nodes.node(nodeId);
let allConnectedNodes = RED.nodes.getAllFlowNodes(node)
If you just want the nodes that come 'before' or 'after' this one, you can do:
let allUpstreamNodes = RED.nodes.getAllUpstreamNodes(node);
let getAllDownstreamNodes = RED.nodes.getAllDownstreamNodes(node)