Session doen't get data

Hey,
for some reason, I can’t get a value with: session.getItem();

My Pagecode:

 if ($w("#uploadButton5").value.length > 0) {
 const Upload = await $w('#uploadButton5').startUpload()
            .then((uploadedFileBergbahn3) => {

                session.setItem("Upload4", "Anything");
            });
    }

My lightbox code, where I want to get the data:

$w.onReady(async function () {

    setInterval(function () {

        let test = session.getItem("Upload1");

        console.log(test);

        if (test === "Anything") {
              Zähler++;
              session.removeItem("Upload1");
        }

    }, 1000);

});

My console.log always gives me null back:

But in my browser storage, I have a value…

Hi Nick :raised_hand_with_fingers_splayed:

One note you need to take, the storage APIs do NOT clear themselves when you get back to the editor, so any saved data with the storage APIs, especially the session, will be cleared only when close the editor.

So, to run QA on session related sections, only use the live site.

Hope this helps~!
Ahmad

Hey Ahmad,

thanks for the node, but I don’t think that the issue.
I’m running the code on my live site and closed my edior, but I still get the same problem.
I also clear my session (with session.clear() ) , before I run my code.

OK, so another note that you might not know, the storage only accept strings as values, so if you’re trying to save numbers, or anything you need to convert them to strings like this:

// Numbers, booleans
String(0); String(true);

// Objects and arrays
JSON.stringify({ key: value }); JSON.stringify([item1, items2]);

And when you get the values, convert them back to their original values.

let x = session.getItem(item);

// Numbers
let number = Number(x);

// Other values
let value = JSON.parse(x);

Hope this helps~!
Ahamd

@ahmadnasriya Hey, I think I found the issue.

Lightbox (works):

let test = await session.getItem("key");

The problem is at my page code:

This works:

session.setItem("key", "I am a String");
wixWindow.openLightbox("Formular");

This doesn’t work:

wixWindow.openLightbox("Formular");
session.setItem("key", "I am a String");

I think the lightbox doesn’t refresh the session data, after I opened it.

Here an example:
Case 1:

This is my session storage, before the lightbox opens. I can use the command

session.getItem("tryout") 

to get the value.

Case 2:

This is my session storage, after the lightbox has been opened. If I use the command

console.log(session.getItem("Upload1"));

I get null as value.

Is there any way to set an session item, after I opened the lightbox?

@yisrael-wix Do you have any ideas, how to send data to a lightbox, after the lightbox was opened?
Seems like the lightbox doesn’t receive session data, when it’s opened.

@nickoertel
What exactly is your aim? What are you trying to achieve?
So if you can’t send data to an opened Lightbox, so why not closing it and open it again with new refreshed data? (denk ans ausblenden & einblenden). :wink:

Here a simple example, of how to send data to Lightbox…
https://russian-dima.wixsite.com/meinewebsite/lightbox

@russian-dima
I want to make an image counter for a form where you can upload up to 12 images. So I need 2 counters for the lightbox.

  1. Count how many images are beeing uploaded ( 1/ X )

  2. Count how many images are already uploded ( X /12 (here’s the problem))

After I opened the lightbox, I don’t receive any session data. So I can’t receive the count from the form page.
Means the counter is always 1/12 and doesn’t change to 2/12, 3/12, …

I don’t want to open/close the box. That wouldn’t look good.
I already looked at your homepage weeks ago :grinning:
But you set the values, before you open the lightbox.

Maybe I should try with an iFrame or a container box, but the background wouldn’t look that good.

@nickoertel any updates on this?

Hey, no I haven’t found a solution yet.
The problem is, that you can’t set a new session Item, after opening the lightbox:

This works:

session.setItem("key", "I am a String");
wixWindow.openLightbox("Formular");

This doesn’t work:

wixWindow.openLightbox("Formular");
session.setItem("key", "I am a String");

You can try it with a container instead of a lightbox.

@nickoertel What is it you are trying to accomplish? What do you mean by “can’t set a new session item after opening the Lightbox”? Why do you want the session item set after?

The proper way to send data to a Lightbox is by passing a data object, as shown in the openLightbox() API :

wixWindow.openLightbox("LightboxName", dataObj);

@yisrael-wix It’s a counter for uploading images.
When I open the lightbox I send the count of how many images are beeing uploaded (in this example 8 images 1/8)

After the first image is uploaded, the sitecode changes a session value from 1 to 2, but I can’t get the new session value from the page.

Seems like the lightbox is not able to get the new session data, after it was opened.