Session data can only be submitted to database if code is placed in $w.onReady, why ?

I have a data I need to be autofilled and submitted to a database. I am using the code below.

import {session} from ‘wix-storage’;

$w.onReady( function () {
let productid = session.getItem(‘productid’);
$w(‘#productidinput’).value = productid;
});

The code is working only when I place it within $w.onReady( function () { , as above.
If i use an export function outside the onReady function, and do a .onClick function the value submitted is blank.
Why ?

1 Like

Hey, @thecandymachinefreel

session.getItem() should work on a button click event. Can you, please, provide the code which doesn’t work for you?
Please, also, provide URL to your web site, and then our authorized staff can take a look at your code.

Ryan
I have the same issue: If I use the setItem in an export function outside, it doesn’t work. The code is at bottom. Did you solve this problem?

import {session} from ‘wix-storage’

export function buttonAgregarInscripcion_click(event)
{
let EmpresaActual = $w(“#textEmpresa”).text;
session.setItem(“stoEmpresa”,EmpresaActual);

let SolicitanteActual = $w(“#textSolicitante”).text;
session.setItem(“stoSolicitante”,SolicitanteActual);

let CapacitacionActual = $w(“#textCapacitacion”).text;
session.setItem(“stoCapacitacion”,CapacitacionActual);
}

Unfortunately, I did not solve this problem, but I found another way to export my data. I apply click function on other elements that are not a button (for mine its a dropdown). Heres the code im using now.

import {session} from ‘wix-storage’;

export function form_click(event, $w) {
session.setItem(‘productnametitle’, $w(“#productnametitle”).text);
session.setItem(‘productid’, $w(“#productid”).text);
session.setItem(‘quantityselect’, $w(“#quantityselect”).value);
session.setItem(‘mainimage’, $w(“#mainimage”).src);
}

$w.onReady( function () {
//TODO: write your page related code here…
});

Thank you Ryan, I’ll try with your solution.
Regards,
Arturo