如何在异常捕获中获取数据库引用?
How to get db reference in the exception catch?
我需要在出现异常时关闭并删除当前的indexeddb
代码行如下所示,
export async function getCurrUser(window) {
...
let openRequest = indexedDB.open('userData',2);
openRequest.onsuccess = e => {
let db = e.target.result;//this is not accessable in the catch block
try{}
catch(e){
//close db and delete it. but I cannot get db reference here
//I can't db.close() here
}
};
}
是否有任何有效的方法可以在异常捕获中获取数据库?
您可以简单地使用 finally
(始终执行)并使用布尔值控制它:
export async function getCurrUser(window) {
...
let openRequest = indexedDB.open('userData',2);
let error = false;
openRequest.onsuccess = e => {
let db = e.target.result;//this is not accessable in the catch block
try{}
catch(e){
error = true;
//print exception
}finally{
if (error){
db.close();
}
}
};
}
我需要在出现异常时关闭并删除当前的indexeddb 代码行如下所示,
export async function getCurrUser(window) {
...
let openRequest = indexedDB.open('userData',2);
openRequest.onsuccess = e => {
let db = e.target.result;//this is not accessable in the catch block
try{}
catch(e){
//close db and delete it. but I cannot get db reference here
//I can't db.close() here
}
};
}
是否有任何有效的方法可以在异常捕获中获取数据库?
您可以简单地使用 finally
(始终执行)并使用布尔值控制它:
export async function getCurrUser(window) {
...
let openRequest = indexedDB.open('userData',2);
let error = false;
openRequest.onsuccess = e => {
let db = e.target.result;//this is not accessable in the catch block
try{}
catch(e){
error = true;
//print exception
}finally{
if (error){
db.close();
}
}
};
}