TransactionInactiveError:无法在“IDBObjectStore”上执行“添加”:事务未处于活动状态
Posted
技术标签:
【中文标题】TransactionInactiveError:无法在“IDBObjectStore”上执行“添加”:事务未处于活动状态【英文标题】:TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction is not active 【发布时间】:2016-01-08 09:12:32 【问题描述】:在这段代码中,当我查看控制台时,我在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();
;
【问题讨论】:
它宁愿在result.invocationResult.resultSet.length
给出错误,因为我没有看到result
变量定义。
【参考方案1】:
当控制权返回事件循环时,事务变为非活动状态,并且仅在该事务内操作的回调中再次处于活动状态。
...
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.
目前尚不清楚您期望这两个交易如何交互。三种方法是:
在范围内对两个商店进行一项交易 - 这为您提供了您可能期望的交易完整性。 每个 add() 都有一个单独的写入事务 记录所有数据以添加到一个数组中,然后在游标迭代/第一个事务完成后创建第二个事务以发出所有 add() 调用。【讨论】:
【参考方案2】:第一眼快速猜测是store1.add 发生的错误,该错误发生在接近末尾的for 循环中。 store1 不能保证具有您期望的值,因为您在 openCursor 的结果之后引用它,这发生在不同的时钟滴答上,这意味着 idb 引擎有机会关闭它,因为它没有找到任何侦听器。
最后从函数内部获取store1(onsuccess函数)。
【讨论】:
以上是关于TransactionInactiveError:无法在“IDBObjectStore”上执行“添加”:事务未处于活动状态的主要内容,如果未能解决你的问题,请参考以下文章