setIn() not updating on first try

I have a javascript query that is using setIn() to update a temporary variable from a form submit. The query get called correctly and I pass in the new value correctly via a trigger. However, the temp variable is not getting update unless I run the query twice. On only the second try does the variable get updated.

Triggering script from button:

const value = form1.data.switch1;
await updateYardPurchaseSetting.trigger({
  additionalScope: {
    category: 'purchaseSettings',
    obj_name: 'testObject',
    value
  }
});

javascript query:

let purchaseSettingScaleViewIndex = currentYardSettings.value.findIndex(x => x.settings_obj_name==obj_name);
await currentYardSettings.setIn([purchaseSettingScaleViewIndex, 'settings_obj'], value);
console.log(currentYardSettings.value)

The script seems pretty straight forward but I might be missing something.

Check out this post:

Hi, Scott.

Idk if I am missing something. I had read that post prior to submitting my own post but at least I got confirmation about it being a good resource; so "thank you" for that.

I am trying to update field, not delete it. In other contexts using setIn is great and I get how to use it.

Do I need to "reload" the temp variable in some way? I tried that previously but something like:

const temp = await currentYardSettings.value;
await currentYardSettings.setValue(temp);

but that was not working, either.

It is curious that this works when firing a second time. That made me think it was a reloading issue.

Or, I could just be missing the point of the referred post.

Is purchaseSettingScaleViewIndex correct?
And is your temp variable an array?

@David_Adams this is a somewhat common "gotcha" with the way that queries are executed in Retool by default. For performance reasons, the default way that queries are executed makes a copy of the application state at the time the query is triggered, but that copy is static and does not update as you make changes within your query.
So when you call setIn and then subsequently look at .value - .value is still the copy that was made when the query started executing.

The reason it appears to work on the second run is that the first execution did actually work and the initial copy for the second run has the updated value that was set by the first run.

If you need to be able to access the updated values, you can enable the setting "Keep variable references inside the query in sync with your app" in the advanced tab - which will make things behave in the way I believe you're expecting.

2 Likes

Ah yes. I always forget about that query setting. That fixed it.

Thanks so much!

2 Likes