RED.nodes.getNode fails to find node

I have two custom nodes in my flow: an "OccupancyTrigger" node, and a configuration node named "schedule". The OccupancyTrigger node uses schedule to detemine when it should be enabled. I want the schedule node to communicate changes in its state to the OccupancyTrigger node's status.

occupancy_trigger.js

module.exports = function(RED) {    
    function OccupancyTrigger(config) {
        RED.nodes.createNode(this, config);
        var node = this;
        node.schedule = RED.nodes.getNode(config.schedule);
        node.schedule.setupStatusReporting(this.id);

schedule.js

node.setupStatusReporting = function(nodeId) {
            node.idsOfNodesToUpdateStatus.push(nodeId);
            if (node.isExpired) {
                node.setNodeExpiredStatus(nodeId);                
            } else if (node.isActive) {
                node.setNodeActiveStatus(nodeId);
            } else {
                node.setNodeInactiveStatus(nodeId);
            }
        }
node.setNodeActiveStatus = function(nodeId) {
            var nodeToSet = RED.nodes.getNode(nodeId);
            if (nodeToSet ) {
                nodeToSet .status({fill: "green", shape: "dot", text: "active"});
            } else {
                node.warn("couldn't find the node!");
            }
        };

I have verified that the id of the OccupancyTrigger node is properly being passed into the schedule node, and that it matches the id displayed on my flow, but I always hit my "couldn't find the node!" logic branch. getNode fails me. Any ideas why?

Hi Chris,

I'm not very sure why this happens, but I 'think' the OccupancyTrigger node is at this stage not available entirely in the runtime yet. But I might be wrong!

I had a question recently, that was a bit similar to yours. At the end I found that I was calling the RED.nodes.getNode a bit too soon, because the node was at the particular moment not fully loaded yet.

Can't you workaround it like this:

node.schedule.setupStatusReporting(this);

and this:

node.setNodeActiveStatus = function(nodeToSet) {
    nodeToSet.status({fill: "green", shape: "dot", text: "active"});
};

Good luck with it!
Bart

Thank you Bart, that's the solution I stumbled upon as well. I'm trying to keep my application as quick and lean as possible, though, so I'm disappointed I can't just pass a simple id as the function parameter. I may keep investigating as time permits and update this thread if I find a better solution.

1 Like