# Oneditsave not Saving Field

**URL:** <https://discourse.nodered.org/t/oneditsave-not-saving-field/73978>\
**Category:** Developing Nodes\
**Created:** [20 January 2023 16:34 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978 "2023-01-20T16:34:21Z")\
**Posts on this page:** 8\
**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:** [20 January 2023 16:34 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/1 "2023-01-20T16:34:21Z")

</div>

Hi, in addition to [Fetch Backend Data](https://discourse.nodered.org/t/fetch-backend-data-in-foreground/73955) I got another problem. I guess I'm simply doing this wrong. 😉 But I simply can't store the fetched data in the node.

I have an `httpAdmin.get('/environ-val/:key', ...)` delivering my data. It is called in `oneditsave()` by calling a global function returning a deferrer. This way the await is realized.

Here're my code extracts:

```auto
defaults: {
	// ...
	key: { value: '' }, 
	envValue: { value: '' }
},

```

```auto
oneditsave: function () {
	// ...
	getEnvironVal(this.key).then(function (ret) {
		console.log(ret); // this works, ret is filled correctly!
		$('#node-input-envValue').val(ret);
	});
	// ...
}

```

```auto
<div class="form-row">
	<!-- ... -->
	<input type="hidden" id="node-input-envValue">
</div>

```

`getEnvironVal()` is a global function to fetch the data:

```auto
function getEnvironVal(val) {
	const def = $.Deferred();

	$.getJSON('/environ-val/' + val)
		.done(function (result) {
			def.resolve(result.value);
		}).fail(function () {
			def.resolve('');
		});

	return def;
}

```

I verified:

- the function after `.then` in `oneditsave` gets executed and contains the correct data! This is great! - `result.value` is just a single string. 🙂
- I made node-input-envValue visible (`type="text"`). When OKing the dialog I can see, that the oneditsave event fills the field right before closing the dialog.

However, after this field `envValue` is still empty. The node doesn't get the "changed" marker. The change to `node-input-envValue` is ignored. I also tried to simply use `this.envValue`, which should work either.

Maybe I have a conceptional misunderstanding here.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 January 2023 16:55 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/2 "2023-01-20T16:55:08Z")

</div>

> [@Flachmann](#):
>
> ```auto
> oneditsave: function () {
> // ...
> getEnvironVal(this.key).then(function (ret) {
> console.log(ret); // this works, ret is filled correctly!
> $('#node-input-envValue').val(ret);
> });
> // ...
> }
> 
> ```

The `thenable` runs async and will be executed after `oneditsave` completed. The only way to pass `ret` into the node at that stage is by accessing the properties of its config. `this` yet may be `undefined` already. Have you tried to do something like...

```auto
oneditsave: function () {
	// ...
	let node = this;
	getEnvironVal(node.key).then(function (ret) { // not necessary to use node here ...
		console.log(ret);

		console.log(this); // to test the assumption for "this"
		console.log(node);

		node.envValue = ret; // ... but here it is necessary!
	});
	// ...
}

```

---

<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:** [20 January 2023 17:51 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/4 "2023-01-20T17:51:03Z")

</div>

(sorry, deleted my previous post, it was wrong and I don't want to leave it)

Ralph, that was it. Very cool! 🙂 Thank you once more! I think I finally owe you a 🍺.

---

<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:** [20 January 2023 22:21 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/5 "2023-01-20T22:21:40Z")

</div>

If you modify node properties asynchronously after returning from oneditsave, then your changes won't be reflected in the edit history and there could be side effects.

Is there a reason you need to do it this way? If you have the `key`, could the node runtime lookup whatever value you need when it starts, rather than trying to do it at edit time?

---

<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:** [23 January 2023 14:20 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/6 "2023-01-23T14:20:32Z")

</div>

I know. In my special case I offer the user to select an environment value, which then, on save, changes another value. I understand, that this is not the very "typical" way. 😉

This topic is a little be tricky, especially as I'm not the world biggest developer. Thanks again to you and Ralph for your extraordinary help!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [23 January 2023 14:40 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/7 "2023-01-23T14:40:28Z")

</div>

@Flachmann just a little thing to watch

> [@Flachmann](#):
>
> ```auto
> $.getJSON('/environ-val/' + val)
> .done(function (result) {
> def.resolve(result.value);
> }).fail(function () {
> def.resolve('');
> });
> 
> ```

I would remove the slash / at the start

If a user has set `httpAdminRoot` to move the editor to a different path, you will get problems assuming the editor is at /.

its safer to do:

```auto
$.getJSON(`environ-val/${val}`)
		.done(function (result) {
			def.resolve(result.value);
		}).fail(function () {
			def.resolve('');
		});

```

---

<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:** [23 January 2023 15:54 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/8 "2023-01-23T15:54:56Z")

</div>

Hi Marcus!

Thank you. Good comment! 😀

---

<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:** [24 March 2023 15:55 UTC](https://discourse.nodered.org/t/oneditsave-not-saving-field/73978/9 "2023-03-24T15:55:35Z")

</div>

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