Dear all,
I’m trying to retrieve a JSON object that has been transformed to a string in order to be stored in Local Storage.
import { local } from 'wix-storage';
$w.onReady(function () {
// let caseInfo = JSON.parse(local.getItem("caseData"));
let caseInfo = local.getItem("caseData");
$w('#text51').text = caseInfo; // if the element is Text
// return JSON.parse(local.getItem("caseData"));
});
What should I do in order to get the key values printed on the screen? thank you so much in advance guys.
How is the data being saved to storage? Keep in mind that setItem() only saves string data, and getItem() only gets string data.
So, if you want to save an object to storage, you need to convert it to a string , and then save the string. And then when you get the data from storage, you are getting a string that has to be parsed to an object .
Hi Yisrael, thank you so much for getting back to me,
I used JSON.stringify to convert to string and then save the string to the local storage. this is the code I used:
export function more_details_btn(event, $w) {
const idOfItem = event.context.itemId;
let caseData;
$w('#repeater1').data.forEach((item) => {
if (item._id === idOfItem) {
caseData = item;
}
});
console.log(caseData); // your item
let selectedItem = local.setItem("itemCaseData", JSON.stringify(caseData));
wixLocation.to('/case-selected');
}
I want to access “caseData” values and then display those in /case-selected. I want to access specifically the values: name and age from “itemCaseData”.
this is a screenshot from localStorage after btn function is triggered:
I tried using JSON.parse to convert the string to an object again and I’m getting this err:
SyntaxError: Unexpected token o in JSON at position 1
Wix code SDK warning: The text parameter of "text51" that is passed to the text method cannot be set to null.
thank you so much in advance, I’m very appreciated
@scarpantonioc Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor. Please include the name of the page involved.