-1

我有以下代码用于从 chrome 30 上的 indexeddb 获取记录。

var IndexedDBStorage = function (name) {
// until we won't need this prefix mess
var indexedDB = window.indexedDB || window.webkitIndexedDB
    || window.mozIndexedDB || window.msIndexedDB;
var IDBTransaction = window.IDBTransaction ||
    window.webkitIDBTransaction;
var db;
// The initialization of our stuff
this.Supported = function () {
    return indexedDB;
};
this.type = function () {
    return "IndexedDB";
};
this.Setup = function () {
    var dbVersion = 1.0;
    var openRequest = indexedDB.open(name, dbVersion);

    //handle setup - as the spec like it
    openRequest.onupgradeneeded = function (e) {
        console.log("running onupgradeneeded");
        var thisDb = e.target.result;
        if (!thisDb.objectStoreNames.contains(name)) {

            var objectStore = thisDb.createObjectStore(name, {
                autoIncrement: false
            });
            objectStore.createIndex("dataKey", "dataKey",
                { unique: false });
        }
    };

    openRequest.onsuccess = function (e) {
        db = e.target.result;
        db.onerror = function (event) {
            alert("Database error: " + event.target.errorCode);
            console.dir(event.target);
        };
        if (db.setVersion) {
            console.log("in old setVersion: " + db.setVersion);
            if (db.version != dbVersion) {
                var req = db.setVersion(dbVersion);
                req.onsuccess = function () {
                    var ob = db.createObjectStore(name, {
                        autoIncrement: false
                    });
                    ob.createIndex("datakey",
                        "datakey", { unique: false });
                    var trans = req.result;
                    trans.oncomplete = function (ex) {
                        console.log("== trans oncomplete ==");

                    };
                };
            }
        }
        console.log(db);

    };
};
this.GetAll = function (callback) {
    console.log(db);
    var transaction = db.transaction([name]); <-- gives error described below
    var store = transaction.objectStore(name);
    var items = [];

    transaction.oncomplete = function (evt) {
        callback(items);
    };

    var cursorRequest = store.openCursor();
    cursorRequest.onerror = function (error) {
        console.log(error);
    };

    cursorRequest.onsuccess = function (evt) {
        var cursor = evt.target.result;
        if (cursor) {
            items.push({ key: cursor.key, body: cursor.value.body });
            cursor.continue();
        }
    };
};


};

如果我从这样的按钮调用它:如果我这样做并通过单击按钮调用它,它工作正常,它工作正常:

 function Init() { <-- called from script tag in index.html
$(document).ready(function () {

    window.dataStore = new Store("data");

});
}

function getAll() { <-- button lick function
window.dataStore.getAll();
}

但是,如果我像这样在初始化后直接调用它

function Init() {
$(document).ready(function () {

    window.dataStore = new Store("data");
window.dataStore.GetAll();

});
}

我收到 Uncaught TypeError 错误:无法调用未定义的方法“事务”

我猜这是因为当我在初始化后直接调用时,尚未从 openRequest.onsuccess 全局设置 db 变量

我该如何解决这个问题,以便正确设置

4

1 回答 1

1

这是由于 indexedDB API 的异步行为。

db 变量尚未分配,因为尚未调用 onsucces。为了解决这个问题,您必须在 openrequest 上调用 onsuccess 时提供回调,或者如果 db 变量未定义,您将不得不延迟 getAll 调用的执行。

于 2013-10-16T09:23:26.530 回答