jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.39k stars 361 forks source link

DOMException: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running. #129

Closed planeth44 closed 5 years ago

planeth44 commented 5 years ago

Hi,

I'm trying to add an index to one of the store, and this the error message I get.

const db = await openDB(myDb, DB_VERSION, {
  async upgrade(db, oldVersion ) {

    const v1Db = db
    if (oldVersion < 1){
      // create stores
    }      
    if (oldVersion < 2){
      const tx = v1Db.transaction(storeName, 'readwrite')
      const store = tx.objectStore(storeName)
      store.createIndex('statusIdx', 'status', { unique: false });
    }      
  }
})
NotWoods commented 5 years ago

You probably need to use the transaction parameter of openDB. I suspect that you're not able to start new transactions while upgrading.

const db = await openDB(myDb, DB_VERSION, {
  async upgrade(db, oldVersion, newVersion, transaction) {
    const v1Db = db
    if (oldVersion < 1) {
      // create stores
    }      
    if (oldVersion < 2) {
      const store = transaction.objectStore(storeName)
      store.createIndex('statusIdx', 'status', { unique: false });
    }      
  }
})
planeth44 commented 5 years ago

Thanks @NotWoods I figured it out as well, after a few hours of letting my brain cool off :)