在javascript中,我这里有一个类来处理请求。我想确保我已经把所有的错误处理设置好了。我想打开到db的连接,并在每个请求时关闭它。到目前为止我有这样的代码
export default class IndexedDBStorage {
#name:string;
constructor(name:string) {
this.#name = name;
}
private async GetDB():Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
// https://javascript.info/indexeddb
const request = window.indexedDB.open(this.#name, 1);
request.onupgradeneeded = () => {
//this.#db = request.result;
if (!request.result.objectStoreNames.contains(this.#name)) {
request.result.createObjectStore(this.#name, {keyPath: 'id', autoIncrement:true});
}
};
request.onerror = () => {
reject("Why didn't you allow my web app to use IndexedDB?! " + request.error?.code);
};
request.onsuccess = () => {
//request.result.onerror = () => {
// Generic error handler for all errors targeted at this database's requests!
// reject("Database error: " + request.error?.code);
//};
resolve(request.result);
};
});
}
async GetAllItems<T>():Promise<Array<T>> {
return new Promise((resolve, reject) => {
this.GetDB().then(db => {
const transaction = db.transaction(this.#name);
const store = transaction.objectStore(this.#name);
const datarequest = store.getAll();
db.close();
datarequest.onsuccess = function() {
resolve(datarequest.result);
};
datarequest.onerror = function() {
reject("Error " + datarequest.error);
};
}).catch(err => {
console.error(err);
});
});
}
}
还想知道,GetDB
函数内部的这部分有什么区别。这是db本身的onerror事件,它是在onsuccess事件中设置的。
//request.result.onerror = () => {
// Generic error handler for all errors targeted at this database's requests!
// reject("Database error: " + request.error?.code);
//};
这部分呢
datarequest.onerror = function() {
reject("Error " + datarequest.error);
};
?
谢谢
编辑:
export default class IndexedDBStorage {
#name:string;
constructor(name:string) {
this.#name = name;
}
private async GetDB():Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = window.indexedDB.open(this.#name, 1);
request.onerror = (event: Event) => {
// this function catches all errors
// all errors in database, transaction or request levels should be caught and handled there
const errorEvent = event as Event & {error:string};
console.error(errorEvent.error);
alert(errorEvent.error);
reject(errorEvent.error);
};
request.onblocked = function () {
// this event shouldn't trigger if we handle onversionchange correctly
// it means that there's another open connection to the same database
// and it wasn't closed after db.onversionchange triggered for it
const message = 'Database is currently blocked, page needs to reload, click ok to reload.';
alert(message);
window.location.reload();
//console.log(message);
//reject(message);
};
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db = request.result;
console.log('idb onupgradeneeded firing');
switch(event.oldVersion) { // existing db version
case 0:
// version 0 means that the client had no database
// perform initialization
console.log('Database currently at version 0 which means that the client had no database');
console.log(`Upgrading to version ${db.version}`);
request.result.createObjectStore(this.#name, {keyPath: 'id', autoIncrement:true});
case 1:
// client had version 1
// update
console.log('Database currently at version 1 which means that the database already exists');
}
};
request.onsuccess = () => {
const db = request.result;
db.onversionchange = function() {
db.close();
const message = "Database is outdated, page needs to reload, click ok to reload.";
alert(message);
window.location.reload();
};
resolve(db);
};
});
}
async GetAllItems<T>():Promise<Array<T>> {
return new Promise((resolve, reject) => {
this.GetDB().then(db => {
const transaction = db.transaction(this.#name);
const store = transaction.objectStore(this.#name);
const datarequest = store.getAll();
db.close();
datarequest.onsuccess = function() {
resolve(datarequest.result);
};
}).catch(err => {
reject(err);
});
});
}
}
1条答案
按热度按时间azpvetkf1#
error
属性