【问题标题】:Transaction undefined error when creating object store in indexedDB在 indexedDB 中创建对象存储时出现事务未定义错误
【发布时间】:2013-02-08 12:27:23
【问题描述】:

我的代码有一些问题,希望您能帮助我。我已经把我的 .open 和所有它的以下代码(unupgradeneede、onsuccess、onerror aso.)放在一个 js 文件中,然后我从另一个 js 文件中调用它。但似乎打开 IndexedDB 和创建对象存储的代码永远不会执行。这是代码

window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB ||    window.msIndexedDB;

contosoData = {

    db: null,

    useDb: function (successCallback) {
        var request = window.indexedDB.open("ContosoData", 2);

        request.onsuccess = function (e) {
            contosoData.db = request.result;
            console.log('indexeddb.success');
            successCallback();
        };

        request.onupgradeneeded = function (e) {
            console.log('indexeddb.upgradeneeded');

            contosoData.db = e.target.result;
            if (contosoData.db.objectStoreNames.contains("bookingStore")) {
                contosoData.db.deleteObjectStore("bookingStore");
                var bookingStore = contosoData.db.createObjectStore("bookingsStore", { keyPath: "id", autoIncrement: true });
                bookingStore.createIndex = ('title', 'title', { unique: false });
                bookingstore.createIndex = ('id', 'id', { unique: true });
            }

            else if (contosoData.db.objectStoreNames.contains("workerStore")) {
                contosoData.db.deleteObjectStore("workerStore");
                var workerStore = contosoData.db.createObjectStore("workerStore", { keyPath: "employeeNr" });
            }

            else if (contosoData.db.objectStoreNames.contains("customerStore")) {
                contosoData.db.deleteObjectStore("customerStore");
                var customerStore = contosoData.db.createObjectStore("customerStore", { keyPath: "orgNr" });
            }

            var machineStore = contosoData.db.objectStoreNames.contains("machineStore", { keyPath: "machineNr" });
            contosoData.db.deleteObjectStore("machineStore");
            var machineStore = contosoData.db.createObjectStore("machineStore");
        };

        request.onerror = function (e) {
            console.log('indexeddb.onerror: ' + e);
        };
        request.onblocked = function (e) {
            console.log('indexeddb.locked');
        };
    }
};

contosoData.useDb(function (db) {
    var transaction = contosoData.db.transaction(['bookingsStore'], 'readwrite');
    var objectStore = transaction.objectStore('bookingsStore');
    console.log(objectStore.name);
});

然后,当我尝试运行它时,当我尝试在另一个 js 文件中创建事务时,我得到一个未定义的事务...我检查了我的 js 文件与我打开的函数IndexedDB 以正确的方式和顺序链接到 html 文件。我还设置了一些断点,看起来 useDb 代码永远不会执行。这是代码

function init() {

    contosoData.useDb();

    calendar.createCalendar();
}


calendar.createCalendar = function () {

    var transaction = contosoData.db.transaction(["bookingStore"], "readwrite");
    var bookingStore = transaction.objectStore("bookingStore");
}

我有什么遗漏吗?我搜索了它,我试图找到一个解决方案,但到目前为止没有成功......

【问题讨论】:

    标签: indexeddb


    【解决方案1】:

    会是这样吗

    function init() {
    
        calendar.createCalendar();
    }
    
    
    calendar.createCalendar = function () {
    
        var transaction = contosoData.db.transaction(["bookingStore"], "readwrite");
        var bookingStore = transaction.objectStore("bookingStore");
    }
    
    
    contosoData.useDb(init);
    

    【讨论】:

    • 感谢您的回答! :-) 我已经尝试过了,不幸的是它不起作用,或者它不能解决我的问题......我进入 contosoData.useDb 没有任何问题,但它没有得到任何回报从我调用 the.open 开始...所以 onsuccess,onupgradeneede (创建所有 objectStores 的地方)永远不会执行,然后我当然会得到一个未定义的事务...但是当我尝试时无法弄清楚出了什么问题打开索引数据库
    【解决方案2】:

    我已经在您的代码中看到了两个问题。

    首先,数据库始终使用版本 2 打开。如果您已经拥有版本 2 的数据库,则永远不会触发 onupgradeneeded 事件。您需要先检查一下(即在 Chrome 中,按 Ctrl-Shift-I 并在 Resources 下检查)。

    二、代码使用这个逻辑

    if (contosoData.db.objectStoreNames.contains("bookingStore")) {
                    contosoData.db.deleteObjectStore("bookingStore");
                    var bookingStore = contosoData.db.createObjectStore("bookingsStore", { keyPath: "id", autoIncrement: true });
                    bookingStore.createIndex = ('title', 'title', { unique: false });
                    bookingstore.createIndex = ('id', 'id', { unique: true });
                }
    

    对于一个空数据库,它肯定不会有任何对象存储,因此 if 块总是被评估为 false。对象存储永远不会被创建,因为它们驻留在一个永远为假的 if 块中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      相关资源
      最近更新 更多