我们正在使用indexedDB在本地存储文件。当浏览器空间不足时,我们会遇到问题(达到可用配额)。似乎事务处理被标记为错误(dom异常配额超出indexdb)。这是一些没有在.onerror句柄中冒泡的东西。我们在下面的示例中,objectStoreRequest.onerror似乎无法正常工作。如果我们从开发工具模拟等于0MB的自定义存储配额,以便触发错误,我们不会看到objectStoreRequest.onerror的日志,这是我们所期望的。为了能够看到有错误,我们必须放置一个setTimeout,它将在几秒钟后被调用,在那一点上,我们将能够看到事务中的错误。我们是否遗漏了什么,是否有人设法正确处理了这个错误?
const simulateStorageQuotaError = () => {// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);
var db = null;
DBOpenRequest.onsuccess = function (event) {
db = DBOpenRequest.result;
// open a read/write db transaction, ready for adding the data
//@ts-ignore
var transaction = db.transaction(["toDoList"], "readwrite");
// report on the success of the transaction completing, when everything is done
transaction.oncomplete = function (event: any) {
console.log('Transaction completed.', event);
};
transaction.onerror = function (event: any) {
console.log('Transaction not opened due to error. Duplicate items not allowed.', event);
};
// create an object store on the transaction
var objectStore = transaction.objectStore("toDoList");
for (let i = 0; i < 100; i++) {
// Create a new item to add in to the object store
var newItem = { taskTitle: `Task Title ${i}`, hours: 19, minutes: 30, day: 24, month: 'December', year: 2013, notified: "no" };
// make a request to add our newItem object to the object store
var objectStoreRequest = objectStore.add(newItem);
objectStoreRequest.onsuccess = function (event: any) {
console.log('Request successful.', event);
console.log('Request successful: transaction', transaction);
setTimeout(() => {
console.log('transaction from setTimeout', transaction);
}, 1000);
}
}
};
// This event handles the event whereby a new version of
// the database needs to be created Either one has not
// been created before, or a new version number has been
// submitted via the window.indexedDB.open line above
DBOpenRequest.onupgradeneeded = function (event) {
//@ts-ignore
var db = event.target.result;
db.onerror = function (event: any) {
console.log('Error loading database.', event);
};
// Create an objectStore for this database
var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });
// define what data items the objectStore will contain
objectStore.createIndex("hours", "hours", { unique: false });
objectStore.createIndex("minutes", "minutes", { unique: false });
objectStore.createIndex("day", "day", { unique: false });
objectStore.createIndex("month", "month", { unique: false });
objectStore.createIndex("year", "year", { unique: false });
objectStore.createIndex("notified", "notified", { unique: false });
};
}
1条答案
按热度按时间x0fgdtte1#
根据本指南:
IndexedDB
如果来源已超过其配额,则尝试写入IndexedDB将会失败。将会呼叫交易的
onabort()
行程常式,并传递事件。此事件会在error属性中包含DOMException
。检查错误name
将会传回QuotaExceededError
。