本文介绍了未捕获的InvalidStateError:无法在“IDBDatabase”上执行“transaction”:正在运行版本更改事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承认我是indexedDB的新手

i must admit that i am very new to indexedDB

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

I wrote a simple code of indexedDB and it is as followed:

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

它给我的错误是未捕获InvalidStateError:无法在'IDBDatabase'上执行'transaction':版本更改事务正在运行。

这是说版本更改交易正在运行但是就我而言研究过,版本更改事务是从 IDBFactory.open 方法制作的,我没有使用过,我已经指定此事务是 readwrite 并且此交易在 onupgradeneeded 那么为什么会出现错误?

It is saying that A version change Transaction is running but as far as i have studied, a version change transaction is made from IDBFactory.open method and i haven't used and i have specified that this transaction is readwrite and this transaction is in onupgradeneeded then why there is an error?

我必须承认我我是indexedDB的新手

i must admit that i am very new to indexedDB

推荐答案

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

The versionchange transaction also allows you to readwrite. You just need to access the transaction created for you within 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 are encountering the error because you are trying to start a second transaction while the version change transaction is still running.

这篇关于未捕获的InvalidStateError:无法在“IDBDatabase”上执行“transaction”:正在运行版本更改事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 04:46