为什么 chrome.storage.local.get return 未定义值?

Why does chrome.storage.local.get return undefined for the value?

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

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

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

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);

   });
  }

提前感谢您的帮助!

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

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