Calling Internal Node Functions

#1

Hi, is possible to add functions to nodes and calling it in the runtime?

For example:

module.exports = function(RED) {
    function SendSomethingNode(config) {
        RED.nodes.createNode(this,config);
        
        node.on('input', function(msg) {

            req.end();
        });

    }
    
    function GetSomethingAboutThisNode() {
        return "something";
    }

    RED.nodes.registerType("sendsomething", SendSomethingNode);
}

When the user do a deploy whis this node in his flow I would like to call the function GetSomethingAboutThisNode. Maybe with something like this RED.getNode(id).GetSomethingAboutThisNode()?

1 Like

#2

@jdiegosierra you can define whatever custom functions you want on the node object, but you do have to get the syntax right.

There are two ways of doing it:

Option 1

function SendSomethingNode(config) {
    RED.nodes.createNode(this,config);
    var self = this;

    this.GetSomethingAboutThisNode = function() {
        return "something"
    }
}

Option 2

function SendSomethingNode(config) {
    RED.nodes.createNode(this,config);
}
RED.nodes.registerType("sendsomething", SendSomethingNode);

SendSomethingNode.prototype.GetSomethingAboutThisNode = function() {
    return "something"
}

In either case, how/where you want to be able to call this function?

1 Like

#4

I want the first one option because I need node params. So having this:

module.exports = function(RED) {
    function SendSomethingNode(config) {
        RED.nodes.createNode(this,config);
        var self = this;

        this.GetSomethingAboutThisNode = function() {
            return "something"
        }
   }
   RED.nodes.registerType("sendsomething", SendSomethingNode);
}

I would like to call it when adminApp.post("/flows",....) is executed so I thought to use it into the post method in ./red/api/admin/flows.js

0 Likes

#5

You can access the node parameters with both options. In Option 2, this inside the GetSomethingAboutThisNode function will be the node itself, so has full access to its properties.

What exactly are you doing? It sounds like you are modifying core runtime code - there may be alternative ways of achieving what you want without having to hack around with the core code.

0 Likes

#6

I need some info about each node and to store them in a database (mongodb) when a deploy is executed by the user. The reason is, I want to store some code in python in each node and when deploy is executed I want to access to this function GetSomethingAboutThisNode() which will build a python script with the node params and return it, then I store it script in mongodb because another programm will read it and execute the function in python.

Something hard I know, but I only has 1 year of experience developing and I'm not sure the best way to face this problem.

Sorry if i have explained a little bad. And so much thanks for your time.

0 Likes

#7

In general nodes should not know or care about other nodes... that is one of the basic tenet for flow based programming... they should be a series of black boxes... - but...
Would it not be simpler to use the admin api to grab the whole flow file that has all the nodes and their properties in and then parse that as required ?

0 Likes

#8

Thank you both. Finally I added a function as @knolleary said and I call it with:

runtime.nodes..getNode(nodeId).GetSomethingAboutThisNode()

0 Likes