将检索到的数据从索引DB存储到一个变量

Store retrieved data from indexed DB to a variable

本文关键字:一个 变量 存储 DB 检索 数据 索引      更新时间:2023-09-26

我使用以下代码从索引DB读取数据并将其保存在变量allDownloadContent

ereaderdownload.indexedDB.getAllTodoItems = function() {
    /*var todos = document.getElementById("todoItems");
    todos.innerHTML = "";
  */
    var db = ereaderdownload.indexedDB.db;
    var trans = db.transaction(["downloadcontent"], "readwrite");
    var store = trans.objectStore("downloadcontent");
    var request = store.get(0);
    request.onsuccess = function(e) {
        console.log(e.target.result);
    };
    // Get everything in the store;
    var cursorRequest = store.openCursor();
    cursorRequest.onsuccess = function(e) {
        var result = e.target.result;
        if(!!result == false)
            return;
        allDownloadContent.push(result);
        result.continue();
    };
    alert("content "+allDownloadContent[0]);
    cursorRequest.onerror = ereaderdownload.indexedDB.onerror;
  };

当我从另一个Javascript文件调用getAllTodoItems方法时,我得到一个警告消息内容未定义

游标请求。onsuccess方法执行异步,我得到未定义。

我不能使用网络工作者,因为它不支持chrome。

我尝试在Jquery承诺。我仍然得到相同的警告信息。

请帮我解决这个问题

到目前为止,所有浏览器都只支持Indexed-db ASync API,您需要做的是为事务oncomplete事件添加一个事件侦听器。此事件将在关闭游标时触发。从那里你可以返回到你的代码:

trans.oncomplete = function (event) {
    console.log('transaction completed');
    yourFunction();
};