Prevent IndexedDB request error from cancelling the transaction

狂风中的少年 提交于 2019-12-04 13:18:31

It seems the logging output from the code I pasted tricked me into thinking that the transaction was aborted, since the transaction.onerror handler was called. What I did not log, though, was whether transaction.onabort was called ... It turned out that a call to event.preventDefault actually was enough!

What confused me was that the error propagated up to the transaction error handler. The transaction was not aborted, though!. It just bubbled up the event after preventing the default action (aborting). To get rid of the message I also need to call event.stopPropagation in order to prevent the event from bubbling up to the transaction's error handler. Note that this is not strictly necessary if you just need the transaction to not be cancelled.

I found the answer by digging around in the W3C docs on IDBRequst and IBTransaction.

It is logical that the transaction is in error when something goes wrong inside the transaction. That is way transactions exist, to keep the database consistent.

In your case you should better first check if the key exists. If it does, do nothing in the other case you can insert the data.

An other solution is opening a transaction for every object you want to save....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!