localStorage.getItem returns null
What are you trying to achieve:
I’m trying to retrieve user data from the local storage
Additional information:
I’ve manually set the
user
variable inside the local storage and I’m trying to retrieve it in the code:
import { local as storage } from 'wix-storage';
$w.onReady(function () {
let user = storage.getItem('user');
console.log('user', user); // null
if (user) {
try {
user = JSON.parse(user);
if (!user || !user.parent) {
// todo redirect to
console.log('redirect to login page as well')
} else {
console.log('User data: ', user);
console.log('User is logged in. Redirect to the parent-hub page')
} catch(e) {}
} else {
// todo: redirect to sign-up page
console.log('User is not logged in. Redirect to login page')
I’ve manually set the user
variable inside the local storage
Does this mean on you have set this value locally on your web browser?
In order to save values in local storage that are accessible via Velo, you should use the Wix Storage API’s setItem() method.
I recommend checking the Velo Example below to see it in use: