With IndexedDB, is it possible to create an object store in the onsuccess event of the window.indexedDB.open?

You can only change the object stores and indices of a database from within the onupgradeneeded event handler function, which operates within the context of an version change read write transaction.

The onupgradeneeded event handler is a different handler than the successful open request event handler. The successful open event handler is not permitted to make changes to the object stores or indices. The success event only happens after the upgrade transaction completes. By that point in time you are attempting a mutation operation (a command to create/delete an object store) on a database that did not allow mutations (outside of the context of a version change transaction).

The solution is to use the onupgradeneeded handler:

var request = indexedDb.open(...);

request.onupgradeneeded = function(event) {
  // do database changes here, e.g.
  var db = event.target.result;
  db.createObjectStore(...);
};

request.onsuccess = function(event) {
  // do database reads/writes here
  // you cannot use createObjectStore here
};

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top