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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With