[英]Local Storage not saving my map. JavaScript, Google Chrome
i am trying to save a map to my local storage and for some reason it never saves. 我试图将地图保存到我的本地存储,由于某种原因它永远不会保存。 ie it just saves as {}.
即它只保存为{}。 The most annoying thing is that I'm sure this worked yesterday!
最烦人的事情是我确信昨天有效!
All help is much appreciated. 非常感谢所有帮助。
export async function getSportsList(){
let liveOddsApiKey = "blahdedah";
try{
let result = await axios (`${dataSourceURL}${sportDataURL}${liveOddsApiKey}`)
let sportList = new Map();
result.data.data.forEach(x=>{
sportList.set(x.title,x.key);
});
console.log("Data Loaded");
console.log(result);
console.log(sportList);
console.log(JSON.stringify(sportList));
localStorage.setItem('sportsData',JSON.stringify(sportList));
console.log(localStorage.getItem('sportsData'));
debugger;
} catch(error){alert(error);}
}
'''
JSON.stringify on a map results in {}. 地图上的JSON.stringify会生成{}。 See https://stackoverflow.com/a/29085474/176868
请参阅https://stackoverflow.com/a/29085474/176868
try this instead... 试试这个......
const sportList = result.data.data.map(x=>({title:x.title, key:x.key}));
..and the rest of your code should work ok. ..其余的代码应该可以正常工作。
You can't JSON.stringify
Map
directly. 你不能直接
JSON.stringify
Map
。 You can store the sportslist
as array and store it in the localStorage. 您可以将
sportslist
存储为数组并将其存储在localStorage中。
Not sure what data you receive in the result
. 不确定您在
result
收到了哪些数据。 I have added dummy data in the following fiddle. 我在下面的小提琴中添加了虚拟数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.