TransactionInactiveError:无法在'IDBObjectStore'上执行'add':事务处于非活动状态

Sw*_*amy 3 indexeddb

在这段代码中,store1.add当我在控制台中查看它时,我遇到了问题TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction is not active.

任何人帮助我,如果可能的话给出一些示例代码.

function indexInitialization(){

     var userDetails = indexedDB.open("dynamicServicess");

     userDetails.onupgradeneeded = function(e) {
         console.log("Upgrading...");
         var thisDB = e.target.result;
         var objectStore=thisDB.createObjectStore("servicess",{keyPath: "ID"});
         var objectStore2=thisDB.createObjectStore("businessareas",{keyPath: "ID"});
         var objectStore3=thisDB.createObjectStore("languages",{keyPath: "ID"});
         var objectStore4=thisDB.createObjectStore("rolenames",{keyPath: "ID"});
         objectStore2.createIndex("language", "LANGLOCALE", { unique: false });
         objectStore.createIndex("businessarea", "BUSINESSAREA", { unique: false });
     objectStore.createIndex("rolename", "ROLENAME", { unique: false });
     }

     userDetails.onsuccess = function(e) {
         console.log("Success!");
         db = e.target.result;
         indexedDbObjectCreation();

     }

     userDetails.onerror = function(e) {
         console.log("Error");      
         console.dir(e);
     }
}

function indexedDbObjectCreation(){
 var transaction = db.transaction(["servicess"],"readwrite");
 var transaction1 = db.transaction(["businessareas"],"readwrite");

 var store = transaction.objectStore("servicess");
 var store1 = transaction1.objectStore("businessareas");

 var req = store.clear();
 var req1 = store1.clear();

 for(i=0;i<result.invocationResult.resultSet.length;i++){               
    store.add({ID:i,SERVICENAME:ss.resultSet[i].SERVICENAME,LANGLOCALE:ss.resultSet[i].LANGLOCALE});    
    }

  var index = store.index("businessarea");
  var singleKeyRange = IDBKeyRange.only("en");  

  index.openCursor(singleKeyRange).onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {
    store1.add({ID:cursor.value.ID,SERVICENAME:cursor.value.SERVICENAME});
       // it is not working error:TransactionInactiveError: Failed to execute
        // 'add' on 'IDBObjectStore': The transaction is not active.
    cursor.continue();
    }
  };

}
Run Code Online (Sandbox Code Playgroud)

Jos*_*ell 5

当控制返回到事件循环时,事务变为非活动状态,并且仅在该事务内的操作的回调中再次处于活动状态.

 ... {
   var transaction = db.transaction(...);
   var transaction1 = db.transaction(...);

   var store = transaction.objectStore(...);
   var store1 = transaction1.objectStore(...);

   // Both transactions active here

   var index = store.index(...);
   index.openCursor(...).onsuccess = function(event) {
      // Async callback from objects from 'transaction' so
      // only 'transaction' is active here, not 'transaction1'
   };

   // No work has been scheduled against 'transaction1' here,
   // so it will attempt to commit as control returns to event 
   // loop here.
}
Run Code Online (Sandbox Code Playgroud)

目前还不清楚你期望两个交易如何互动.三种方法是:

  • 在范围内与两个商店进行一次交易 - 这为您提供了您可能期望的交易完整性.
  • 为每个add()都有一个单独的写事务
  • 记录要添加到数组中的所有数据,然后在光标迭代/第一个事务完成后创建第二个事务以发出所有add()调用.


Jos*_*osh 1

乍一看,错误发生在 store1.add 处,该错误发生在 for 循环接近结尾处。store1 不能保证具有您期望的值,因为您在 openCursor 结果之后引用它,这发生在不同的时钟周期,这意味着 idb 引擎有机会关闭它,因为它没有找到任何侦听器。

从最后的函数(onsuccess 函数)中获取 store1。