John Smith
John Smith

Reputation: 6269

Cannot create second objectStore IndexedDB

I basically have this code to generate a new database with one objectStore:

DB.open_or_create({table: 'photos', key: 'url'});

And the main code:

var DB = {
 open_or_create : function(p){
    var openRequest = indexedDB.open("new_app",1);
    openRequest.onupgradeneeded = function(e) {
        var thisDB = e.target.result;
        if(!thisDB.objectStoreNames.contains(p.table)) {
            thisDB.createObjectStore(p.table,{ keyPath: p.key });
        }
    }
    openRequest.onsuccess = function(e) {DBTABLES = e.target.result}
    openRequest.onerror = function(e) {console.log(e)}
 }

Then i tried to add a second objectStore with the command:

  DB.open_or_create2({table: 'users',key: 'id'});

And basically the same code only with another database version number:

open_or_create2 : function(p){
    var openRequest = indexedDB.open("new_app",2);
    openRequest.onupgradeneeded = function(e) {
        var thisDB = e.target.result;
        if(!thisDB.objectStoreNames.contains(p.table)) {
            if(p.key){
                thisDB.createObjectStore(p.table,{ keyPath: p.key });
            }else{
                thisDB.createObjectStore(p.table)
            }
        }
    }
    openRequest.onsuccess = function(e) {DBTABLES = e.target.result}
    openRequest.onerror = function(e) {console.log(e)}
}

But as i can see in the console only the first table gets created! Also when i try to add data to the patients table i get this error:

Uncaught NotFoundError: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found. 

What do i wrong? Thanks!

Upvotes: 1

Views: 5263

Answers (1)

dgrogan
dgrogan

Reputation: 2720

In the course of development you probably already increased new_app to version 2. Bump it up to version 3 in your above code and it will work. If not, add a openRequest.onblocked handler and add console.log lines to all your event handlers to see which one is getting fired.

Upvotes: 2

Related Questions