【问题标题】:Why does chrome.storage.local.get return undefined for the value?为什么 chrome.storage.local.get 返回 undefined 的值?
【发布时间】:2021-07-03 00:13:07
【问题描述】:

我正在开发 Google 扩展程序并使用 Google 存储。

以下是我保存和访问已保存数据的代码。我检查了控制台,数据保存正确。但是,当我使用 chrome.storage.local.get 访问此数据时,控制台返回未定义的值而不是保存的值。

保存代码:popup.html 的 save.js

function save() {
    # the id is for a textarea in another html page
    var text = document.getElementById("text").value;

    chrome.storage.sync.set({"txt": text}, function(){
      
       # console states the correct value
       console.log('Value is set to ' + text);
     });

   # this is the page I want to retrieve the saved data in 
   window.location.replace("anotherHTMLpage.html");

}

访问代码:retrieve.js for anotherHTMLpage.html

function retrieve() {
    chrome.storage.sync.get(["txt"], function(data) {
     
      # console says "value currently is undefined"
      console.log('Value currently is ' + data.key);

   });
  }

提前感谢您的帮助!

【问题讨论】:

    标签: google-chrome-extension undefined google-chrome-storage


    【解决方案1】:

    Chrome 将数据保存在您设置的密钥下。在您的示例中,您将其保存为txt,因此它将在data.txt 下而不是data.key

    查看文档了解更多详细信息https://developer.chrome.com/docs/extensions/reference/storage/#usage

    【讨论】:

    • 您为我指出了正确的问题,为我节省了很多时间!上帝保佑你。
    猜你喜欢
    • 2018-07-29
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多