简体   繁体   English

为什么没有在现有的indexedDB上创建新的objectStore?

[英]Why are my new objectStores not being created on existing indexedDB?

I have an existing indexedDb and I need to add a couple of new objectStores to it. 我有一个现有的indexedDb,并且需要向其中添加几个新的objectStores。

I'm increasing the version number and, in the onupgradeneeded function, building the objectStores if they don't already exist. 我正在增加版本号,并在onupgradeneeded函数中构建对象存储(如果尚不存在)。

            var db;
            var dbName = 'dev';
            var request = window.indexedDB.open(dbName, 2);

            request.onblocked = function(event) {
                // 
            };

            request.onerror = function (event) {
                //
            };

            request.onsuccess = function (event) {
                db = request.result;
            };

            request.onupgradeneeded = function (event) {
                var db = event.target.result;
                console.log(`Upgrading to version ${db.version}`);
                if (!db.objectStoreNames.contains('existing_store_1')) {
                    var existingStore1 = db.createObjectStore('existing_store_1', {keyPath: 'id'});
                }
                if (!db.objectStoreNames.contains('existing_store_2')) {
                    var existingStore2 = db.createObjectStore('existing_store_2', {keyPath: 'id'});
                }
                if (!db.objectStoreNames.contains('new_store_1')) {
                    var newStore1 = db.createObjectStore('new_store_1', {keyPath: 'id'});
                }
                if (!db.objectStoreNames.contains('new_store_2')) {
                    var newStore2 = db.createObjectStore('new_store_2', {keyPath: 'id'});
                }
            };

            return db;

When I refresh the site, the database version does jump to the new version number, but the new objectStores aren't created. 当我刷新站点时,数据库版本确实跳到了新的版本号,但是没有创建新的objectStores。 If I delete the database and refresh, it creates the database with all of the objectStores, including the new ones. 如果删除数据库并刷新,它将创建包含所有objectStore(包括新对象)的数据库。

Obviously I need new users to have the entire database built with all objectStores, and existing users to have the new objectStores built without deleting their existing database. 显然,我需要新用户使用所有objectStore构建整个数据库,而现有用户需要在不删除现有数据库的情况下构建新的objectStore。

What am I doing wrong? 我究竟做错了什么?

I have seen this How to add a new Objectstore to an existing indexeddb but it doesn't answer my question. 我已经看到了如何将新的对象存储添加到现有的indexeddb中,但是它无法回答我的问题。

My first guess, it is related to return db; 我的第一个猜测,与return db; . That signals that you might be trying to use this synchronously, and are not waiting for things to happen, and you might be holding a reference to the prior instance of the IDBDatabase variable instead of the new one. 这表明您可能正在尝试同步使用此功能,而不是等待事情发生,并且您可能持有对IDBDatabase变量的先前实例的引用,而不是对新实例的引用。 I'm partly saying this not because it is specific to your code but just because the fact that idb ops tend to be async and async is tough for many devs and is a common problem. 我之所以说这一点,并不是因为它特定于您的代码,而是因为idb ops倾向于异步并且异步对于许多开发人员来说都是困难的,这是一个普遍的问题。

Try the following and see if it helps. 请尝试以下操作,看看是否有帮助。 First, no more global var db variable usage. 首先,不再使用全局var db变量。 Second, write a function for connecting that looks like this: 其次,编写一个连接函数,如下所示:

function connect(callbackFunction) {
   var openRequest = ...;
   openRequest.onupgradeneeded = ...;
   openRequest.onsuccess = function(event) {
     var db = event.target.result;
     callbackFunction(db);
   };
}

// Now to use it, you can only refer to db from within the callback

connect(function onConnected(db) {
  console.log('Use the db variable inside the function');
});

console.log('never try to use the db variable here');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM