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