# Calling Internal Node Functions

**URL:** <https://discourse.nodered.org/t/calling-internal-node-functions/5669>\
**Category:** Core Development\
**Created:** [11 December 2018 09:59 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669 "2018-12-11T09:59:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdiegosierra](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdiegosierra/32/6990_2.png) [@jdiegosierra](https://discourse.nodered.org/u/jdiegosierra)\
**Post date:** [11 December 2018 09:59 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/1 "2018-12-11T09:59:14Z")

</div>

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

For example:

```auto
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()?

---

<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:** [11 December 2018 10:06 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/2 "2018-12-11T10:06:50Z")

</div>

@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

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

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

```

## Option 2

```auto
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?

---

<div class="post-metadata">

**Author:** ![jdiegosierra](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdiegosierra/32/6990_2.png) [@jdiegosierra](https://discourse.nodered.org/u/jdiegosierra)\
**Post date:** [11 December 2018 10:54 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/4 "2018-12-11T10:54:36Z")

</div>

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

```auto
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

---

<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:** [11 December 2018 10:57 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/5 "2018-12-11T10:57:34Z")

</div>

> [@jdiegosierra](#):
>
> I want the first one option because I need node params. So having this:

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.

> [@jdiegosierra](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![jdiegosierra](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdiegosierra/32/6990_2.png) [@jdiegosierra](https://discourse.nodered.org/u/jdiegosierra)\
**Post date:** [11 December 2018 11:28 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/6 "2018-12-11T11:28:35Z")

</div>

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.

---

<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:** [11 December 2018 14:32 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/7 "2018-12-11T14:32:38Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jdiegosierra](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdiegosierra/32/6990_2.png) [@jdiegosierra](https://discourse.nodered.org/u/jdiegosierra)\
**Post date:** [14 December 2018 13:24 UTC](https://discourse.nodered.org/t/calling-internal-node-functions/5669/8 "2018-12-14T13:24:39Z")

</div>

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

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