Reputation: 349
When I try to insert data I get the following error:
"Uncaught DOMException: Failed to execute 'put' on 'IDBObjectStore': The transaction has finished."
My code:
request.onsuccess = function(e) {
var ip="kaka";
var res;
console.log("success");
db = request.result;
tx = db.transaction("List","readwrite");
store = tx.objectStore("List");
index = store.index("Name");
displayData(function oncompleted(value) {
console.log('The value is', value);
store.put({
Name: $('#card').val(), IP: value
});
});
}
function displayData(myCallbackFunction) {
//code
myCallbackFunction(set);
}
Upvotes: 0
Views: 823
Reputation: 9663
That error means that your transaction has already finished before your store.put
call. In IndexedDB, transactions commit automatically:
Transactions are tied very closely to the event loop. If you make a transaction and return to the event loop without using it then the transaction will become inactive. The only way to keep the transaction active is to make a request on it. When the request is finished you'll get a DOM event and, assuming that the request succeeded, you'll have another opportunity to extend the transaction during that callback. If you return to the event loop without extending the transaction then it will become inactive, and so on. As long as there are pending requests the transaction remains active. Transaction lifetimes are really very simple but it might take a little time to get used to. A few more examples will help, too. If you start seeing
TRANSACTION_INACTIVE_ERR
error codes then you've messed something up.
Basically this means, you can't do other unrelated async stuff and then make a request on a transaction.
Looking at your code, this shouldn't be a problem because displayData
synchronously calls myCallbackFunction
. I'm guessing the code you posted is just a simplified example though, and is not the real code that is producing the error. In your real code, displayData
is probably asynchronously executing its callback.
Upvotes: 3