【问题标题】:Store retrieved data from indexed DB to a variable将检索数据从 indexedDB 存储到变量
【发布时间】:2012-12-26 10:21:12
【问题描述】:

我正在使用以下代码从索引数据库中读取数据并将其保存在变量 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 方法时,我收到一条警告消息 content undefined

由于 cursorRequest.onsuccess 方法执行异步,我变得未定义。

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

我在 Jquery 中尝试过 promise。我仍然收到相同的警报消息。

请帮助我解决问题。

【问题讨论】:

  • Chrome 不支持网络工作者是什么意思? caniuse.com/webworkers
  • 我的意思是同步 indexeddb api
  • 我猜你想在你编码return的回调中提醒。那时数据是可用的。

标签: javascript jquery synchronization indexeddb


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多