Global Variables work in Preview but not Published

Question:
Why is my WIX storage “Local” data not working after publishing? (ie. “Session” storage fails the same)

Product:
Using Editor, Velo, CMS Collections, repeaters, etc.

What are you trying to achieve:
The code works perfectly in “Preview” mode but when I “Publish” it it fails (most of the time – but very rarely it seems to work, but can’t trace why.)

I am using “local” variables to transfer a code (eg “SL1”) and description (eg “Severn Lodge – Early years” between 2 pages on my website. The from page is a Dynamic page, the second is NOT dynamic.

A “button on.Click” on the first page pulls some data from a repeater field, saves the2 fields to either Local (but Session storage failed as well). The button is defined to link to Page 2.

Code from page 1:
The code works perfectly in “Preview” mode but when I “Publish” it it fails (most of the time – but very rarely it seems to work, but can’t trace why.)
I am using “local” variables to transfer a code (eg “SL1”) and description (eg “Severn Lodge – Early years” between 2 pages on my website. The from page is a Dynamic page, the second is NOT dynamic.
A “button on.Click” on the first page pulls some data from a repeater field, saves the2 fields to either Local (but Session storage failed as well). The button is defined to link to Page 2.

// Code from page 1:
local.setItem(“gbl_coll_cd”, coll_cd);
local.setItem(“gbl_coll_desc”, coll_desc);

// Code from page 2:
let coll_cd = local.getItem(“gbl_coll_cd”)
let coll_desc = local.getItem(“gbl_coll_desc”)
$w(‘#text29’).text = "raw global vars " + local.getItem(“gbl_coll_desc”) + local.getItem(“gbl_coll_cd”) // debug

console.log("leaving Ar-Col-Tab")
console.log("raw global vars " 
        + local.getItem("gbl_coll_desc") 
        + local.getItem("gbl_coll_cd")  )

In Preview Mode, this works perfectly. Below is Console.Logs

console

… and shot of actual displayed page showing successful results.

Preview results pic

Following is what I see live once Published. Notice the special debug field I put in says both Code field and Description fields are “nulls”.

Live failed

Works in preview every time. Fails almost every time after Published.
Any clues???
Mike

Hey Mike,

I cannot determine what might be the cause of the issue, but I’d like to suggest an alternative method.
I’d recommend that you use wix-location-frontend.queryParams API instead of the storage APIs. This works seamlessly for use cases like yours - where you have to transfer data from one page to the other.

Hi,
I dug around earlier and found the location.to function that you are talking about but I rationalized why that wouldn’t fix my problem - but now that you are suggesting it I’ll give it a try.
I don’t know how wix handles code initiated from the same button that links to a page - whether it does the code first before the link or “who knows”. But, while trouble shooting this problem I did try using 2 buttons, one to run my code , and a second to do the link. I got the same results…

But I will try your suggestion with some optimism, and send my results.
Thanks so much. Mike

Hi,
Pages are working very will after using your suggestion of using “location.to” function in Velo code to do the actual linking to the next page. FYI, in doing some testing, it does appear that when you click a button with an element (chain) link assigned to it that that link process starts at the same time that the onClick velo code is running. This will give questionable results as you implied on your message. By running the “location.to” in sequence after the other Velo code the result is more predictable.
Thanks again for the help. I will mark the issue as resolved.

Mike

Glad to hear that it worked out! (: