未捕获的TypeError:使用indexedDB时无法调用未定义的方法'transaction'

时间:2013-10-15 15:04:50

标签: javascript indexeddb

我有以下代码用于从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();

});
}

我收到错误 未捕获的TypeError:无法调用未定义的方法'transaction'

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

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

1 个答案:

答案 0 :(得分:1)

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

尚未分配db变量,因为尚未调用onsucces。要解决此问题,您必须在openrequest上调用onsuccess时提供回调,否则,如果db变量未定义,则必须延迟执行getAll调用。