简体   繁体   中英

chrome.storage.local.set() is not working properly

I'm having a difficult time dealing with chrome storage. 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.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. 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?


Finally figured it out, so my problem is that the value of info.userName is not JSON compatible. 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...");

  • to get item value from Local Storage localStorage.getItem("message");

  • to remove item from Local Storage localStorage.removeItem("message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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