Node-Red don´t set node properties default values on load

I have a question to Node properties default value:

My frist version of node html file:

    defaults: {
        val1: { value: 10, required: true, validate: RED.validators.number() },
    },

node_old

I add new node properties to html file:

    defaults: {
        val1: { value: 10, required: true, validate: RED.validators.number() },
        val2: { value: 20, required: true, validate: RED.validators.number() },
    },

Node-Red reports invalid validation:

node_new

On dialog val2 is undefined:

node_old2

Why is val2 is undefined when I load the flow ?
Why is val2 not default 20 on load ?
This circumstance is very disruptive on extend nodes.
The user must update all nodes.

Hi @iiot2k,
Indeed when you afterwards add extra properties to a node, you simetimes need to add some extra code to make sure existing nodes don't fail. Because you need to make sure those nodes keep working fine in following circumstances:

  1. When the config screen of an existing node is opened.
  2. When the node runs without the config screen has been opened.

I have described my way of working here. Hopefully that can answer some of your questions already.

Bart

1 Like

Hi Bart,
thank you for answer.
Why set not the Node-Red runtime and editor the properties default values on load ?
The default values are set only when you drag and drop on editor.
There is a other solution.
Pack the properties on an object array.
But this is very complicated to program.
I have used this in my gpio node.

1 Like

Don't know that. I always do it like in the link I have posted..

I think @knolleary Nick know the answer. :thinking:

The runtime doesn't know anything about the defaults properties in the .html file. So your node's js code must handle the case where newly added properties are not defined.

The editor doesn't automatically add new properties - the defaults only apply to newly added instances of the nodes. That's just how it works.

If I understand correctly, the node programmer must set the properties in oneditprepare if undefined to default value.
And if the properties in js file are undefined then set to default value.
To prevent the invalid validation in html file, the user must open and close all nodes with new version.

or add a custom validation function that allows for the (old) undefined value.

It exist a validation function in html file.
This is called on validate properties and returns true or false.
Can we add a new function setdef for example ?
This is called before validate and returns the default value if undefined.
Like this:

val2: { value: 20, required: true, setdef: function(v) { return (v === undefined) ? 20 : v; }, validate: RED.validators.number() },

1 Like

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