我的问题涉及捕获可能导致HTML显示的错误,存储在indexedDB中的数据以及用于写入数据的映射键不同步。
在下面的代码中,如果函数参数'key'或'data'出现问题,以至于try / catch中的一个数据库请求在发出请求之前就失败了,T将abort()总是导致T.onerror函数执行?
目标是,如果数据库事务失败,则在发出请求之前或之后,然后将为下一个事务回滚RAM中维护的映射键的增量,该增量将被写入数据库。
如果catch中的T.abort()不会总是导致T.onerror运行,那么我认为映射键的回滚应该移到catch语句中。
谢谢。
function add_element_and_data( key, data )
{
let T = DB_open.base.transaction( [ 'objectStore_1', 'objectStore_2' ], 'readwrite' ),
n = T.objectStore( 'objectStore_1' ),
u = T.objectStore( 'objectStore_2' ),
req_n, req_u;
T.oncomplete = () => { /* Code to add HTML element to reflect new database entry */ }; // close T.oncomplete
T.onerror = ( v ) =>
{
v.stopPropagation(); // Stop propagation up to the database error level in the database open block.
/* Code to rollback database mapping keys held in RAM as function properties. */
}; // close T.onerror
try
{
/* Code to increment database mapping keys held in function properties. */
req_n = n.get( key );
req_n.onsuccess = function()
{
let r = req.result;
/* Update r. */
n.put( r );
};
// Add the new note entry.
p.add( data );
} // close try
catch(e) { T.abort(); }
} // close function