In this code I am facing problem at store1.add
when I look in console it says TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction is not active
.
Any one help me, if possible give some sample code.
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();
}
};
}
Transactions become inactive when control returns to the event loop, and are only active again in callbacks from operations within that transaction.
... {
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.
}
It's unclear how you are expecting the two transactions to interact. Three approaches would be:
Quick guess at first glance is the error occurs at store1.add that occurs within the for loop near the end. store1 isn't guaranteed to have the value you expect because you're referencing it after the result of openCursor, which occurs on a different clock tick, meaning that the idb engine has the opportunity to close it because it did not find any listeners.
Get store1 from within the function at the end (the onsuccess function).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With