出现异常时需要关闭并删除当前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
}
};
}
有什么有效的方法可以在异常捕获中获取db吗?
最佳答案
您可以简单地使用finally
(始终执行)并用 bool 值控制它:
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();
}
}
};
}
关于javascript - 如何在异常捕获中获取数据库引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57306598/