0

I am using this code to get chrome.storage.local value:

function getCachedAuthToken(times){
    var cachedToken = "";
    if(times > 3){
        return "";
    }
    chrome.storage.local.get('cruiseToken', (result) => {
        if (result.cruiseToken) {
            console.log("token:" + result.cruiseToken);
            cachedToken = result.cruiseToken;
            return result.cruiseToken;
        } else {
            console.log("token:wwwww");
            fetchAuthToken("+8615683761628","12345678");
            ++times;
            getCachedAuthToken(times);
        }
    });
    console.log("cachedToken:" + cachedToken);
    return cachedToken;
}

now I am sure the result.cruiseToken have a string value. But the outer cachedToken value is always "". what should I do get the inner value ?

Dolphin
  • 29,069
  • 61
  • 260
  • 539

1 Answers1

1

Try to replace cachedToken = result.cruiseToken; by cachedToken=JSON.stringify(obj);
And make sure you define it in the manifest:

    "permissions": [
      "unlimitedStorage", 
      "clipboardRead", 
      "clipboardWrite", 
      "nativeMessaging" , 
      "storage"
    ],
zoldxk
  • 2,632
  • 1
  • 7
  • 29
  • the cachedToken still "", `tokenstringify:{"cruiseToken":"eyJ1c2VyX2lkIjoiNyJ9.9416ae3e26f5fc0d1fabbeffd93a3e42c3fba18a16174927846068fb30"}` @zoldxk – Dolphin Apr 04 '21 at 00:09
  • Yes, I aready have permission to using storage, I am sure @zoldxk – Dolphin Apr 04 '21 at 00:19