为什么我会出现错误?
我的代码:
var idb = window.indexedDB || // Use the standard DB API
window.mozIndexedDB || // Or Firefox's early version of it
window.webkitIndexedDB; // Or Chrome's early version
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
var dbName='nameDb';
var idbRequest=idb.open(dbName,'4.67' /*,dbDescription */);
idbRequest.onsuccess=function (e) {
debugger
var db=e.target.result;
if (!db.objectStoreNames.contains('chat')){
co=db.createObjectStore('chat',{'id':100});
};
if (!db.objectStoreNames.contains('iam')){
co1=db.createObjectStore('iam');
};
};
idbRequest.onerror = function (e) {
debugger
};
未捕获的 InvalidStateError:无法在“IDBDatabase”上执行“createObjectStore”:数据库未运行版本更改事务。索引.html:37 idbRequest.onsuccess
您无法在
objectStore
方法中创建 onsuccess
。您只能在 upgradeneeded
活动中执行此操作。
引自文档:
当您创建新数据库或增加现有数据库的版本号(通过在打开数据库时指定比之前更高的版本号)时,将触发
事件。在此事件的处理程序中,您应该创建此版本的数据库所需的对象存储onupgradeneeded
请参阅文档。
try creating your objectStore on onupgradeneeded event:
request.onupgradeneeded = (event: any) => {
const db = event.target.result;
if(!db.objectStoreNames.contains(this.currentObjectStore)){
db.createObjectStore(this.currentObjectStore);
}
};