Uncaught InvalidStateError: Failed to complete transaction in 'IDBDatabase': version change transaction in progress

I have to admit I'm very new to indexing DB

I wrote simple indexedDB code and it looks like this:

 function go(){var req = window.indexedDB.open("Uploader", 1), db; req.onerror=function(e){console.log('Error')}; req.onsuccess = function(e){db=e.target.result;}; req.onupgradeneeded = function(e){console.log(db); db=e.target.result;db=e.target.result; var os = db.createObjectStore('Files', {keyPath:"files"}); os.createIndex('text', 'text_file', {unique:false}) var trans = db.transaction(['text'], "readwrite"); var objectstore= trans.objectStore("text"); var addreq = objectstore.add('Instructions.js'); addreq.onsuccess = function(e){console.log('Success!');console.dir(e)} }} 

the error it gives me is Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.

He says that A version change Transaction is running , but as far as I learned, the version change transaction is made from the IDBFactory.open method, and I did not use it, and I indicated that this readwrite transaction, and this transaction in onupgradeneeded , why does it occur error?

I have to admit I'm very new to indexing DB

+6
source share
3 answers

The versionchange transaction also allows you to overwrite. You just need to access the transaction created for you in the onupgradeneeded function.

 function go() { var req = indexeddb.open(...); req.onupgradeneeded = function(event) { // note that event.target === req === this, use whatever you like var db = event.target.result; // createObjectScore implicitly uses the versionchange txn running // here, without telling you, basically a convenience function var objectStore = db.createObjectStore(...); // the important part that i am describing in this answer, // grab the handle of the versionchange txn // that is already running and was created for you // note again that event.target === req, you could just do // req.transaction or this.transaction here. // note this is a property of the open request, not a method. do NOT // confuse this with the method transaction() that is used to create a // new transaction. var txn = event.target.transaction; // note that txn.objectStore(...) will work here, because the // createObjectStore call earlier guarantees the store exists here // within the implicit upgrade txn var addRequest = txn.objectStore(...).add('value'); // side note: if in previous line we did: // var objectStoreRetrievedFromTxn = txn.objectStore(...); // then that variable is equal to the same variable returned from // db.createObjectStore earlier in this function. Both are simply handles // (references, pointers, whatever you want to call it) to the store. // kind of dumb, but you could do this just to log something addRequest.onsuccess = function() {console.log('Success!');}; }; // called once upgrade txn completes (if it even needed to run), // and db is opened req.onsuccess = function(event) { console.log('upgrade txn completed and db is now connected'); // here, create whatever readwrite or readonly txns you want, note these // txns are separate from and different than the versionchange txn from // before, because that is a unique transaction only available within // onupgradeneeded. however, both versionchange and readwrite are similar // in that both support calls to put or add }; } 

You encountered an error because you are trying to start a second transaction while the version change transaction is still in progress.

+7
source

Before attempting to load the object store, you need to check the completion of the version change transaction:

 request.onupgradeneeded = function(event) { db = event.target.result; var store = db.createObjectStore('Files', {keyPath:"files"}); var transaction = event.target.transaction; transaction.oncomplete = function(event) { // Now store is available to be populated } } 
+4
source

when changing the version you do not need to specify the scope of the transaction. This is always all repositories of prenset objects. transaction.objectStore('text') Here you are trying to open a repository of objects with the index name, this will not work. First of all, you need to go to the object store if you want to access the index.

Adding data must be done at the object store.

 function go(){var req = window.indexedDB.open("Uploader", 1), db; req.onerror=function(e){console.log('Error')}; req.onsuccess = function(e){db=e.target.result;}; req.onupgradeneeded = function(e){ console.log(db); db=e.target.result; var trans=e.target.transaction; var os = db.createObjectStore('Files', {keyPath:"files"}); os.createIndex('text', 'text_file', {unique:false}) var objectstore= trans.objectStore("Files"); var addreq = objectstore.add('Instructions.js'); addreq.onsuccess = function(e) {console.log('Success!');console.dir(e)} }} 

Try

+1
source

Source: https://habr.com/ru/post/1235960/


All Articles