Forum Academy Marketplace Showcase Pricing Features

Session & Local Storage

Hi, I able store the values in local storage. But when i enter next value it replace the old one. So, I need to store list of values in Local storage. Wheather it is possible?

Yes it’s possible but you need to convert the object/properties to a JSON string first though because local storage can only store key/value pairs.

Do you have an easy way to bring it back to a bubble thing?

I don’t know your use case but yes this can be done. The main idea when moving things outside of a Bubble object and back into one is the unique id. It is the field that allows you to get the Bubble object back. So if you were wanting to store a Bubble object in local storage (or a list of objects) you would just want to store their ids. You can then retrieve the objects using the Toolbox plugin Expression like this:

Assuming Person was stored as follows:
Screenshot 2022-09-17 at 2.44.02 PM

And MultilineInput’s value is the unique id of the Bubble object Person.

I’m not certain that accomplishes what you might hope it would, but it is the answer to if you can you move a Bubble object in and out of local storage easily.

1 Like

Pretty neat. By any chance, would you have any idea on how to retrieve an API response (via the API connector that actually transforms the responses as “things”)… Save it as JSON (instead of a thing) in local storage… And use that “json” as “thing” when needed in the app? (hope it’s clear hehe)

I’ve never tried that before but my hunch is that it can be done. I’ll take a look at it when I have a minute.

1 Like

I confirmed this is possible. I’ve already shown you how to use the Toolbox plugin to store and retrieve the key/value pair from local storage so I’m not going to revisit that here. The important part to cover is how to get the JSON from the API and then convert it back to the API data type.

To do that you want to use this code in the toolbox plugin serverscript action to get the JSON from the API data:

var api_data = get_original(properties.thinglist1).map((x) => {return x["val"]});

Then you can retrieve the JSON out of local storage and using the run server script action, specify the api type as the Return type and return it as a list.

Screenshot 2022-09-20 at 3.47.31 PM

Use the results in the next step to set a state of the same type so that it is available on your page.

I realize that the details on this are spread out over several posts. I’m thinking of making a future post to capture all of this in a single thread as I’ve seen similar API/JSON questions asked around the forums quite a bit. I’ll link it back here if I end up doing that.

1 Like

Will test asap… Thanks so much for your time! Def a game changer for me and others using external dBs.

I have a WAY easier solution coming soon for you.

2 Likes

:eyes:

Interesting

1 Like

yep… so the “json to thing (api thing)” part, that didn’t work lol… Don’t know what i’m doing wrong but i can’t translate my json back to a single or list bubble api thing.

I can’t look at it today but I will try to dig into it more and post something soon. Did you check your local storage and confirm the JSON object is being stored as you expected something like this:

i didn’t do the local storage part (because it’s quite easy to set it up), i actually put the text value of the json object (same as your screenshot) in a state and trying to save it in another state as a bubble “api thing”.

it’s really the transformation from json text to thing part that i didn’t get hehe