Problem with Local Storage

I am having some problems with Local Storage.
I have a Products Table and an Invoice Table.
I have the operator highlight a product and click on a "Insert in Invoice" button.
The code then saves the item name, the price and the tax code as local storage them it goes through the code in the attached graphic to insert these into fields in the Invoice Table.
The code works fine if I run it stand-alone in development mode but it only works intermittently when I run it in real time.
Any thoughts?

:slight_smile:

You should call {{localstorage.getItem('SelItem')}}

Jonathan:
Actually the syntax in ReTool Documentation is a little different from the Javascript official documentation.

The ReTool Docs say:
localStorage.setValue ("SelPrice", table2.selectedRow.data.PRICE)

The Javascript Docs say:
localStorage.setItem ("SelPrice", table2.selectedRow.data.PRICE)

Mike

Hey @mdsmith1!

Would you mind showing us exactly how you're setting the values in localStorage, along with how the query is being triggered? I'm wondering if there might be some kind of race condition that might be going on that could be fixed by ensuring the two actions happen sequentially (e.g. in a JavaScript query).

I'm also curious as to why you're using localStorage in particular, could you share more about that as well?

Kabirdas:
I am using LocalStorage to store a Product Number, so I can retrieve some product info and insert it into an invoice.
I only need the storage for a few milliseconds.
If there is some other way of doing this, please let me know.
Mike

Got it, it might help if we can see exactly how you have the flow configured. Some approaches that can be helpful are to either chain queries together using success events or write a single JavaScript query that uses await to ensure the actions happen sequentially. If you can send over screenshots of the rest of the queries/handlers you're using I can be more specific with the syntax!

Kabirdas:

Can you explain about the "await" feature. I feel that if I can get the sequences to slow down that may solve the problem.

Mike

When you're writing JavaScript within Retool and make some change to your app model or trigger a query that operation typically happens asynchronously. For instance, if you have an invoice query that references a productInfo query and you trigger both like this:

productInfo.trigger();
invoice.trigger();

Then the invoice query will trigger before productInfo is done, meaning you'll be accessing old data! Instead, you can write

await productInfo.trigger();
invoice.trigger();

Which will cause the script to wait until productInfo is done before triggering invoice.

A very similar thing can be done by having invoice be triggered on success of productInfo though, so often times it's not necessary to worry about scripts:

You can use await on a bunch of different methods though, and if you're interested about how it works more generally in JavaScript you can check out these docs!