删除IndexedDb数据库并通过单击按钮重新创建,单击2或3次后不起作用

jdzmm42g  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(359)

使用下面的代码,当按下html按钮时,我删除了整个数据库,并从头开始创建它。(我需要这个。)但在单击几次按钮(2-3次单击)后,它会损坏,不删除,并卡住。
因为我有autoIncrement列,所以只重置数据而不删除数据库对我来说不起作用。我必须删除数据库并重新创建它,以便在autoIncrement中重置它。

let db;

        //DELETE DATABASE
        var req = indexedDB.deleteDatabase("AdminDatabase");
        req.onsuccess = function () {
            console.log("Deleted database successfully");
        }
        
        //CREATE DATABASE
        const request = window.indexedDB.open("AdminDatabase", 3);
        request.onupgradeneeded = function (event) {
            db = event.target.result;
        
            if (event.oldVersion < 1) {
                var objectStore = db.createObjectStore("floors", { keyPath: "id", autoIncrement: true });
            }
        
            if (event.oldVersion < 2) {
                var objectStore = db.createObjectStore("rooms", { keyPath: "id", autoIncrement: true });
            }
        
            if (event.oldVersion < 3) {
                var objectStore = db.createObjectStore("components", { keyPath: "id", autoIncrement: true });
                let index = objectStore.createIndex('componentFloorId, componentRoomId', ['componentFloorId', 'componentRoomId']);
                // let index2 = objectStore.createIndex('roomId', 'roomId', { unique: false});
        
            }
        
            // objectStore.createIndex("name", "name", { unique: false });
        
        };
        
        //CONNECT DATABASE
        const request2 = indexedDB.open("AdminDatabase");
        request2.onerror = (event) => {
            console.error("Connect Db Error!");
        };
        request2.onsuccess = (event) => {
          
            db = event.target.result;

            //DB TRANSACTIONS
           ...
           ...
           ...
        };
uinbv5nw

uinbv5nw1#

在**//DB TRANSACTIONS刷新页面后用location.reload();**解决了我的问题。

相关问题