Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to execute 'createObjectStore' on 'IDBDatabase'

Tags:

why do I have a error ?

My code:

var idb = window.indexedDB ||      // Use the standard DB API           window.mozIndexedDB ||   // Or Firefox's early version of it           window.webkitIndexedDB;  // Or Chrome's early version  var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;  var dbName='nameDb';  var idbRequest=idb.open(dbName,'4.67' /*,dbDescription  */);  idbRequest.onsuccess=function (e) {     debugger      var db=e.target.result;       if (!db.objectStoreNames.contains('chat')){         co=db.createObjectStore('chat',{'id':100});     };      if (!db.objectStoreNames.contains('iam')){         co1=db.createObjectStore('iam');     };  };  idbRequest.onerror = function (e) {     debugger }; 

Uncaught InvalidStateError: Failed to execute 'createObjectStore' on 'IDBDatabase': The database is not running a version change transaction. index.html:37 idbRequest.onsuccess

like image 816
Michael Phelps Avatar asked Jun 24 '14 08:06

Michael Phelps


People also ask

What is keyPath in IndexedDB?

In short, the indexName is how we want to the "field" or "column" (index) will be named in our "table" (Object Store) and the keyPath is the property of the stored object that contains the value of the field.

What is IndexedDB objectstore?

The IDBObjectStore interface of the IndexedDB API represents an object store in a database. Records within an object store are sorted according to their keys. This sorting enables fast insertion, look-up, and ordered retrieval. Note: This feature is available in Web Workers.


1 Answers

You can't create an objectStore in an onsuccess method. You can only do this in a upgradeneeded event.

Quote from docs:

When you create a new database or increase the version number of an existing database (by specifying a higher version number than you did previously, when Opening a database), the onupgradeneeded event will be triggered. In the handler for this event, you should create the object stores needed for this version of the database

See documentation.

like image 78
Dick van den Brink Avatar answered Sep 28 '22 04:09

Dick van den Brink