# Node Creation oneditprepare

**URL:** <https://discourse.nodered.org/t/node-creation-oneditprepare/72840>\
**Category:** Developing Nodes\
**Created:** [28 December 2022 15:50 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840 "2022-12-28T15:50:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flachmann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/flachmann/32/60457_2.png) [@Flachmann](https://discourse.nodered.org/u/Flachmann)\
**Post date:** [28 December 2022 15:50 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840/1 "2022-12-28T15:50:29Z")

</div>

Hi,

I used the first node example from the Node-RED documentation to wrote a first little node. Thanks for that easy to understand example anyway. I enhanced the example to not only use msg.payload as input, but a configurable field. This should be an easy exercise.

My field is named `inField`. It and a hidden `-type` field are created as documented. In `oneditprepare` I defined some additional input parameters:

```auto
    $('#node-input-inField').typedInput({
    	type: 'msg',
    	types: ['str', 'msg', 'flow', 'global', 'jsonata', 'env'],
    	typeField: '#node-input-inField-type',
    });

```

```auto
    <div class="form-row">
    	<label for="node-input-inField"><i class="fa fa-cog"></i> Input</label>
    	<input type="text" id="node-input-inField" placeholder="...">
    	<input type="hidden" id="node-input-inField-type">
    </div>

```

I also wrote an on event to fetch the change of the type.

```auto
    $('#node-input-inField').on('change', function (event, type, value) {...}

```

The edit mask looks well, event is triggered. 🙂

However, it's not clear to me how to store and handle input field type changes and let Node-RED handle this. Maybe I didn't find it, or the docu is somewhat short on this part, or it must be that obvious, that it's not even worse to mention it.

Let's say the user selects type 'jsonata' and value '"ABCD"' (I know it's a constant that could be done by a string, but hey, it's just an example) . The event is fired. Where and how do I have to store the new type, so that it remains? NR stores the value automatically, but not the changed type.

_And in addition, a 2nd question:_  
I guess I have to perform the data interpretation myself. So, in my .js, on input, I get the value "ABCD" in the config object, but not the type (str, msg, jsonata, ...).

Are there examples how to do this?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 December 2022 16:36 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840/2 "2022-12-28T16:36:40Z")

</div>

> [@Flachmann](#):
>
> Where and how do I have to store the new type, so that it remains? NR stores the value automatically, but not the changed type.

This should also be stored automatically for you. Perhaps you are setting this manually?

> [@Flachmann](#):
>
> I guess I have to perform the data interpretation myself.

There are helper functions to do this: [RED.util.evaluateNodeProperty](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.evaluateNodeProperty)

e.g...

```auto
function myNode(config) {
    RED.nodes.createNode(this, config)
    const node = this
    node.inField = config.inField || "payload"
    node.inFieldType = config.inFieldType || "msg"

    node.on('input', function (msg) {
        let inField
        RED.util.evaluateNodeProperty(node.inField, node.inFieldType, node, msg, (err, value) => {
            if (err) {
                node.error("Unable to evaluate inField", msg)
                node.status({ fill: "red", shape: "ring", text: "Unable to evaluate inField" })
                return//halt flow!
            } else {
                inField = value
            }
        })
        // do stuff with inField
    })
}
RED.nodes.registerType("myNode", myNode)

```

> [@Flachmann](#):
>
> Are there examples how to do this?

[https://nodered.org/docs/api/ui/typedInput/#examples](https://nodered.org/docs/api/ui/typedInput/#examples)

---

<div class="post-metadata">

**Author:** ![Flachmann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/flachmann/32/60457_2.png) [@Flachmann](https://discourse.nodered.org/u/Flachmann)\
**Post date:** [29 December 2022 10:53 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840/3 "2022-12-29T10:53:09Z")

</div>

Hi Steve,

thanks for your quick answer. I still believe it's a very trivial issue, but I don't get it. I wonder, if it's some syntax issue here.

> This should also be stored automatically for you. Perhaps you are setting this manually?

No, the HTML code is as shown above with the node-input- field and node-input--type field. The typedInput as well only contains these three displayed fields. There is no oneditsave, or something.

_Versions:_  
node.js v18.12.1  
Node-RED v3.0.2

```auto
oneditprepare: function () {
	$("#node-input-inField").typedInput({
		type: "msg",
		types: ["str", "msg", "flow", "global", "jsonata", "env"],
		typeField: "#node-input-inField-type"
	});
	$("#node-input-inField").on("change", function (event, type, value) {
		console.log(type + " - " + value);
});

```

The list of types is available, so the `oneditprepare` is used. I just detected, that with the above definition the field type always open with 'str', not with 'msg'. Probably because it's the first entry in the list of types. Maybe that's the problem and the type is ignored?

Changing the field type in the dialog works. If changing e.g. to 'jsonata' the JSONata editor is available. But after clicking 'Done' and re-open the type changes back to 'str'.

> There are helper functions to do this: RED.util.evaluateNodeProperty

Thanks a lot. That link was new to me. I'll have a look. 🙂

> [TypedInput Widget : Node-RED](https://nodered.org/docs/api/ui/typedInput/#examples)

I knew these examples. They were my source.

---

<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:** [29 December 2022 11:11 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840/4 "2022-12-29T11:11:42Z")

</div>

Have you added `inField-type` to the `defaults` property of the node? Without that, the editor won't know to store/retrieve the value of that input.

It isn't necessary to add your own change listener on it, if everything is configured properly

---

<div class="post-metadata">

**Author:** ![Flachmann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/flachmann/32/60457_2.png) [@Flachmann](https://discourse.nodered.org/u/Flachmann)\
**Post date:** [29 December 2022 11:43 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840/5 "2022-12-29T11:43:50Z")

</div>

Hi Nick,

wonderful! **That was it!**

Thanks a lot for your quick and qualified help. Really appreciate it.

Maybe it's worse to to add a note to the documentation.

---

<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:** [12 January 2023 11:44 UTC](https://discourse.nodered.org/t/node-creation-oneditprepare/72840/6 "2023-01-12T11:44:05Z")

</div>

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