Stuck using config node multiple times

Hey there! I'm stuck understanding one simple mechanism. Please enlighten me.

I use a config node and as far as I learned from the docs, I get the content with RED.nodes.getNode. So I do:

Let me describe the situation with the option "bool". In JS, I do:

module.exports = function(RED) {
    function configNode(config) {
        RED.nodes.createNode(this,config)
		this.config = config
	}
    RED.nodes.registerType("mytemplate-config",configNode);
	
	function workingNode(config) {
		RED.nodes.createNode(this,config)
		let parms = RED.nodes.getNode(config.config)
		...
	RED.nodes.registerType("mytemplate",workingNode);
}
...

Which gives me the console output:

configNode {
  id: 'f476dc6e65b0a93d',
  type: 'mytemplate-config',
  z: undefined,
  g: undefined,
  _closeCallbacks: [],
  _inputCallback: null,
  _inputCallbacks: null,
  name: 'myparms',
  wires: [],
  _wireCount: 0,
  send: [Function: NOOP_SEND],
  config: {
    id: 'f476dc6e65b0a93d',
    type: 'mytemplate-config',
    name: 'myparms',
    valueType: 'bool',
    value: 'true',
    _users: [ '01a83f7c216f97b8', 'd2ef1c167664e574' ]
  }
}

One parameter field config.value is a typedInput allowing ["bool","num","str"]. I learned that "bool" and "num" are also stored as strings and I have to convert it first. Now in JS I do:

...
if (parms.config.valueType === "bool") {
	parms.config.value = parms.config.value === "true"
}
...

Resulting in:

valueType: 'bool',
value: true,

Great. But when calling the node a second time, it fails. If I add a console.log before and after conversion, I see that the second node, before conversion, holds the value from the conversion of the first node.

BEFORE CONVERSION
configNode {
  ...
  config: {
    ...
    valueType: 'bool',
    value: 'true',
    ...
  }
}
AFTER CONVERSION
configNode {
  ...
  config: {
    ...
    valueType: 'bool',
    value: true,
    ...
  }
}
BEFORE CONVERSION
configNode {
  ...
  config: {
    ...
    valueType: 'bool',
    value: true,
    ...
  }
}
AFTER CONVERSION
configNode {
  ...
  config: {
    ...
    valueType: 'bool',
    value: false,
    ...
  }
}

Find the complete code here: GitHub - danube/template

Doh... kinda hard to explain, hope you got it. Now may you guide me to the right path please... Regards! Thomas

Hi Thomas, you could take the easy way out and change your test to:

...
if (parms.config.valueType === "bool") {
	parms.config.value = ( parms.config.value === "true" || parms.config.value === true )
}
...

Doubtless there are more elegant ways but that seems simple, and direct if a little brutal. :slight_smile:

Hi and thanks for taking time!

There is just one major thing I don't understand. I call the same node twice with the same configuration node(!). In the code I create a copy of the config with

let parms = RED.nodes.getNode(config.config)

and then do the manipulation:

parms.config.value = parms.config.value === "true"

This works for the first call of the node, where the type is being converted from STRING 'true' to BOOL true. But on the second call, I see a BOOL before conversion. Remember: Same code, same config node! For some weird reason it seems to me that the second node call holds the value converted by the first call.

If you modify params then you are modifying the config node and any other users of that node will see the change.

It would be cleaner to to any property validation/cleanup in the config node itself so all consumers of it will benefit and don't need to do it themselves.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.