gpt4 book ai didi

indexeddb - 交易无效错误: Failed to execute 'add' on 'IDBObjectStore' : The transaction is not active

转载 作者:行者123 更新时间:2023-12-02 01:15:22 25 4
gpt4 key购买 nike

在此代码中,当我查看控制台时,我在 store1.add 遇到问题,它显示 TransactionInactiveError: 无法在“IDBObjectStore”上执行“add”:事务未激活

任何人都可以帮助我,如果可能的话,请提供一些示例代码。

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

}

最佳答案

当控制权返回到事件循环时,事务将变为非事件状态,并且仅在该事务内的操作的回调中再次事件。

 ... {
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() 调用。

关于indexeddb - 交易无效错误: Failed to execute 'add' on 'IDBObjectStore' : The transaction is not active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com