我正在尝试使用IndexedDB重新创建一个我的MySQL/PHP项目(一个存储书签、带有重点颜色、文件夹和快速拨号/固定部分的“新标签页”样式页面),作为客户端存储解决方案(希望将其移动到Chrome扩展程序)。虽然我认为我对JS有足够好的理解,但我无法弄清楚为什么以下代码只返回一个空数组。从我的观察中可以看出,“response”数组在函数开始时已经声明,因此应该可以被其中的子函数访问。插入控制台日志表明,数组已成功地被推送到迭代游标所创建的对象,但是一旦游标完成,response就变为空白。
我在这里是否有些愚蠢,漏掉了什么重要的东西吗?
// Specify index and key value OR omit for all
function getItems(ind,key) {
var response = [];
var transaction = db.transaction(["items"],"readonly");
var store = transaction.objectStore("items");
// If args are omitted - grab all items
if(!ind | !key) {
var cursor = store.openCursor();
cursor.onsuccess = function(e) {
var res = e.target.result;
if(res) {
var r = {
"name": res.value['name'],
"url": res.value['url'],
"folder": res.value['folder'],
"colour": res.value['colour'],
"dial": res.value['dial'],
"order": res.value['order']
};
response.push(r);
res.continue();
}
return response;
}
} else {
// If both args are specified query specified index
store = store.index(ind);
var range = IDBKeyRange.bound(key, key);
store.openCursor(range).onsuccess = function(e) {
var res = e.target.result;
if(res) {
var r = {
"name": res.value['name'],
"url": res.value['url'],
"folder": res.value['folder'],
"colour": res.value['colour'],
"dial": res.value['dial'],
"order": res.value['order']
};
response.push(r);
res.continue();
}
return response;
}
}
}
我在这里是否有些愚蠢,漏掉了什么重要的东西吗?