为什么 chrome.storage.local.get 返回 undefined 的值? [英] Why does chrome.storage.local.get return undefined for the value?
问题描述
我正在开发 Google 扩展程序并使用 Google 存储.
I am working on a Google Extension and using Google Storage.
以下是我用于保存和访问已保存数据的代码.我检查了控制台,数据保存正确.但是,当我使用 chrome.storage.local.get 访问此数据时,控制台返回未定义的值,而不是保存的值.
Below is my code for saving and accessing the saved data. I checked the console and the data is saving correctly. However, when I access this data using chrome.storage.local.get console returns undefined for the value and not the value saved.
保存代码:save.js for popup.html
Code for Saving: 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
Code for Accessing: 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
.
Chrome saves the data under the key you set it as. In your example, you saved it as txt
, so it will be under data.txt
not data.key
.
查看文档了解更多详情https://developer.chrome.com/docs/extensions/reference/storage/#usage
这篇关于为什么 chrome.storage.local.get 返回 undefined 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!