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

**URL:** <https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343>\
**Category:** Developing Nodes\
**Created:** [20 February 2021 10:51 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343 "2021-02-20T10:51:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [20 February 2021 10:51 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/1 "2021-02-20T10:51:16Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2b5c3f52c884ac5e2de6df4439cac703dbaa1584.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/294d62f45deb7c6fb2d336a09b859eb4b6c74621.png)

On dialog val2 is undefined:

![node_old2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6ce1da58742fca3d0283b83c0206fd5477b87367.png)

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.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [20 February 2021 11:17 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/2 "2021-02-20T11:17:21Z")

</div>

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](https://discourse.nodered.org/t/upgrading-nodes-specifically-my-own/2641/11). Hopefully that can answer some of your questions already.

Bart

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [20 February 2021 11:51 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/3 "2021-02-20T11:51:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [20 February 2021 12:58 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/4 "2021-02-20T12:58:54Z")

</div>

> [@iiot2k](#):
>
> Why set not the Node-Red runtime and editor the properties default values on load ?

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

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [21 February 2021 10:41 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/5 "2021-02-21T10:41:27Z")

</div>

I think @knolleary Nick know the answer. 🤔

---

<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:** [21 February 2021 11:01 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/6 "2021-02-21T11:01:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [21 February 2021 11:41 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/7 "2021-02-21T11:41:01Z")

</div>

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.

---

<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:** [21 February 2021 11:44 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/8 "2021-02-21T11:44:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [21 February 2021 11:56 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/9 "2021-02-21T11:56:39Z")

</div>

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() },

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 April 2021 11:57 UTC](https://discourse.nodered.org/t/node-red-don-t-set-node-properties-default-values-on-load/41343/10 "2021-04-22T11:57:11Z")

</div>

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