chrome.storage.local.get导致“未定义”什么时候叫 [英] chrome.storage.local.get results in "Undefined" when called
问题描述
我正在构建一个chrome扩展,我需要在本地保存一些数据;所以我使用了存储API 。我必须运行简单的示例并保存数据,但是当我将其与我的应用程序集成时,它无法找到数据并且给我未定义结果。
I'm building a chrome extension, and I needed to save some data locally; so I used the Storage API . I got to run the simple example and save the data, but when I integrated it with my application, it couldn't find the data and is giving me "Undefined" result.
这是我的代码:
function saveResults(newsId, resultsArray) {
//Save the result
for(var i = 0; i < resultsArray.length; i++) {
id = newsId.toString() + '-' + i.toString();
chrome.storage.local.set({ id : resultsArray[i] });
}
//Read and delete the saved results
for(var i = 0; i < resultsArray.length; i++) {
id = newsId.toString() + '-' + i.toString();
chrome.storage.local.get(id, function(value){
alert(value.id);
});
chrome.storage.local.remove(id);
}
}
推荐答案
首先感谢Rob和BreadFist以及所有人。我发现了为什么我的代码无效。
Storage.Set不接受该键为整数,即使您尝试将该键转换为字符串,它也不会起作用。所以我在每个键之前添加了一个常量字符,它起作用了。这是我的代码。
First of All thanks to Rob and BreadFist and all you guys. I found out why my code wasn't working. Storage.Set doesn't accept the key to be an 'integer' and even if you try to convert that key to be a 'string' it won't work too. So I've added a constant character before each key and it worked. Here's my code.
function saveResults(Id, resultsArray) {
var key = Id.toString();
key = 'a'.key;
chrome.storage.local.set({key : resultsArray});
}
function Load(Id) {
var key = Id.toString();
key = 'a'.key;
chrome.storage.local.get(key, function(result){
console.debug('result: ', result.key);
});
}
这篇关于chrome.storage.local.get导致“未定义”什么时候叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!