How to quote a config node

Hi,
I tried config node following Configuration nodes : Node-RED (nodered.org), I wrote remote-server.html

// remote-server.html

<script type="text/javascript">
    RED.nodes.registerType('remote-server',{
        category: 'config',
        defaults: {
            host: {value:"localhost",required:true},
            port: {value:1234,required:true,validate:RED.validators.number()},
        },
        label: function() {
            return this.host+":"+this.port;
        }
    });
</script>

<script type="text/html" data-template-name="remote-server">
    <div class="form-row">
        <label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label>
        <input type="text" id="node-config-input-host">
    </div>
    <div class="form-row">
        <label for="node-config-input-port"><i class="fa fa-bookmark"></i> Port</label>
        <input type="text" id="node-config-input-port">
    </div>
</script>

and remote-server.js

module.exports = function(RED) {
    function RemoteServerNode(n) {
        RED.nodes.createNode(this,n);
        this.host = n.host;
        this.port = n.port;
    }
    RED.nodes.registerType("remote-server",RemoteServerNode);
}

and I want to quote the remote server node in test node, here are the .js and .html file:
test.html

<script type="text/javascript">
    RED.nodes.registerType('test',{
        category: 'function',
        color: '#a6bbcf',
        defaults: {
            server: {value:"", type:"remote-server"}
        },
        inputs: 1,
        outputs: 1,
        icon: "file.svg",
        label: function() {
            return this.name||"test";
        }
</script>

<script type="text/html" data-template-name="test">
     <div class="form-row">
         <label for="node-input-server"><i class="fa fa-tag"></i> Server</label>
         <input type="text" id="node-input-server">
     </div>
</script>

<script type="text/html" data-help-name="test">
    <p>A simple node for configuring
</script>

test.js

module.exports = function(RED) {
    function TestNode(config) {
        RED.nodes.createNode(this,config);
        var node = this;
       
        this.server = RED.nodes.getNode(config.server);
        node.log(this.server);
        node.log("host =");
        node.log(this.server.host);
        node.log("port =");
        node.log(this.server.port);
        node.on('input', function(msg) {
            node.send(msg);
        });
    }
 RED.nodes.registerType("test",TestNode);

When I started node-red, it showed that this.server is null. How can i use the config node ? Thanks.

Hello @abu !
Welcome to this forum!

As the flow is still in the process of being created, your config node might not exist so far.
Have you tried to get it in node.on('input', ... )? There it should be setup & present.

I tried in node.on, here is the code:

 node.on('input', function(msg) {
            this.server = RED.nodes.getNode(config.server);
            node.log(this.server);
            if( this.server )
            {
                node.log("host =");
                node.log(this.server.host);
                node.log("port =");
                node.log(this.server.port);
            }

            node.send(msg);
        });

and this.server is still null. And in the edit page, the input box of server has no select values as a down list in the box.
17047031401796

The runtime does try to ensure all config nodes are started before any flow nodes are created.

@abu have to logged the value of config.server to ensure it's actually set to the id of the config node you expect?

1 Like

Thanks knolleary,
I logged config.server and found it was empty.

Fixed?

Maybe the code pasted but it's </label>

Hi GogoVega,
The </la has been changed to </lable>, the input box of server has no select values, and config.server is empty.

You reversed the place of the second l with e => label instead of lable
Don't forget to reload/refresh your browser after restarting NR

Hi GogoVega,
Sorry for that, it is a spelled fault. In the code, it is label, not lable. and thanks for pointing out that.

No worries,
I don't see anything wrong. Have you refreshed your browser?

This must be done after each restart of NR in order to load the new code into the browser

I refreshed the browser, and and node.log(config.server) is empty.

The problem comes from the input - is it formatted correctly (select input with options)?

I think that the select options of server are from node remote-server, because i quote it as:

defaults: {
            server: {value:"", type:"remote-server"}
        },

Is that correct ?

Yes it's correct

I mean this:

should look like this:

Yes, the input box of server should have select options, but now the input box has no select options at all, and I can not figure out why.

Do you have to declare config nodes in package.json like other nodes? Can't remember.

Yes, same way

Can you try RED.nodes.getType("remote-server") in the browser console

I added RED.nodes.getType("remote-server") to test.js, it showed:
1704761758221

and I added a function node in browser console, like

after clicking the inject node, the browser console showed

function : (error)
"TypeError: Cannot read properties of undefined (reading 'getType')"

and the function node just like this:

msg.payload = RED.nodes.getType("remote-server");
return msg;

it seems that RED.nodes.getType("remote-server") is not defined in node-red.

Hi,
I added remote-server to package.json, and fixed the issue. Thanks for your help.

I said in the browser console but regardless the goal was to check that your config node has been loaded.