简体   繁体   English

chrome.storage.local.set() 无法正常工作

[英]chrome.storage.local.set() is not working properly

I'm having a difficult time dealing with chrome storage.我在处理 chrome 存储时遇到了困难。 I've looked at the latest documentation for chrome storage and have the following code (this is a code snippet inside an ajax call success function, info.userName is a value returned by my backend program):我查看了 chrome 存储的最新文档并具有以下代码(这是 ajax 调用成功函数中的代码片段,info.userName 是我的后端程序返回的值):

chrome.storage.local.set({ "userName": info.userName }, () => {
    chrome.storage.local.get("userName", function(data) {
        console.log(data.userName)
    });
});

but the console.log() always returns undefined, while I'm pretty sure that info.userName has a value.但是 console.log() 总是返回 undefined,而我很确定 info.userName 有一个值。 I've also tried this which is not working as well:我也试过这个,但效果不佳:

chrome.storage.local.set({ "userName": info.userName });
chrome.storage.local.get("userName", function(data) {
    console.log(data.userName)
});

It seems that the set function doesn't save anything locally, does anyone has any thought about this?似乎 set 函数在本地没有保存任何内容,有人对此有任何想法吗?


Finally figured it out, so my problem is that the value of info.userName is not JSON compatible.终于想通了,所以我的问题是 info.userName 的值与 JSON 不兼容。 After I changed it the first code snippet above works well.在我更改它之后,上面的第一个代码片段运行良好。 Thanks for the comments below!感谢以下评论!

you can do it by these way.....你可以通过这些方式做到......

  • to set item in Local Storage localStorage.setItem("message", "Hi, I am Local Storage...");在本地存储中设置项目localStorage.setItem("message", "Hi, I am Local Storage...");

  • to get item value from Local Storage localStorage.getItem("message");从本地存储中获取项目值localStorage.getItem("message");

  • to remove item from Local Storage localStorage.removeItem("message");从本地存储中删除项目localStorage.removeItem("message");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM