javascript 未捕获的 InvalidStateError:无法在“IDBDatabase”上执行“事务”:版本更改事务正在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33709976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:49:48  来源:igfitidea点击:

Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running

javascripttransactionsindexeddb

提问by anni saini

i must admit that i am very new to indexedDB

我必须承认我对 indexedDB 很陌生

I wrote a simple code of indexedDBand it is as followed:

我写了一个简单的代码,indexedDB如下所示:

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 is giving me is Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.

它给我的错误是 Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.

It is saying that A version change Transaction is runningbut as far as i have studied, a version change transaction is made from IDBFactory.openmethod and i haven't used and i have specified that this transaction is readwriteand this transaction is in onupgradeneededthen why there is an error?

这是说,A version change Transaction is running但据我研究,版本更改事务是从IDBFactory.open方法中进行的,我没有使用过,我已经指定了这个事务readwrite和这个事务,onupgradeneeded那么为什么会出现错误?

i must admit that i am very new to indexedDB

我必须承认我对 indexedDB 很陌生

回答by Josh

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

versionchange 事务还允许您读写。您只需要访问在 onupgradeneeded 函数中为您创建的事务。

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 are encountering the error because you are trying to start a second transaction while the version change transaction is still running.

您遇到错误是因为您在版本更改事务仍在运行时尝试启动第二个事务。

回答by Velojet

You need to check for the completion of the version change transaction before attempting to load the object store:

在尝试加载对象存储之前,您需要检查版本更改事务的完成情况:

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
            }
    }

回答by Kristof Degrave

in a version change you do not need to specify a scope for the transaction. This is always all the prenset object stores. transaction.objectStore('text')here you are trying to open an object store with the name of the index, this isn't going to work. You need to go to the objectstore first if you want to access the index.

在版本更改中,您不需要指定事务的范围。这始终是所有的 prenset 对象存储。 transaction.objectStore('text')在这里,您尝试使用索引名称打开对象存储,这是行不通的。如果要访问索引,则需要先转到对象存储库。

Adding data needs to be done on the objectstore.

添加数据需要在对象存储上完成。

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)}
}}

Give this a try

试试这个