1
在IndexedDB的onupgradeneeded()
事件中,我试图更新对象存储中的每条记录。为了更新他们,我需要先瓶坯的异步操作,但这会导致升级交易变得无效,出现错误在升级事件中异步更新IndexedDB
Failed to execute 'update' on 'IDBCursor': The transaction is not active.
在后续代码我模拟的异步操作与setTimeout()
let openRequest = indexedDB.open('myDb', 1);
openRequest.onupgradeneeded = function (versionEvent) {
let db = versionEvent.target['result'];
let upgradeTransaction = versionEvent.target['transaction'];
if(versionEvent.oldVersion < 1) {
let objStore = db.createObjectStore('sample');
objStore.add('one', '1');
objStore.add('two', '2');
}
if(versionEvent.oldVersion >= 1) {
let getCursor = upgradeTransaction.objectStore('sample').openCursor();
getCursor.onsuccess = (e) => {
let cursor = e.target['result'];
if (cursor) {
setTimeout(() => {
cursor.update(cursor.value + ' updated');
cursor.continue();
})
}
}
}
};
https://plnkr.co/edit/DIIzLduZT1dwOEHAdzSf?p=preview
如果你运行这个plunker它将初始化IndexedDB的。然后,如果将版本号增加到2并再次运行,则会出现错误。
如果我的更新依赖于异步操作,如何更新升级事件中的IndexedDB?
谢谢!我想我选择3并且调用'open()'没有版本号。 – rob