Javascript 未能执行';createObjectStore';在';IDB数据库';
为什么我会出错 我的代码:Javascript 未能执行';createObjectStore';在';IDB数据库';,javascript,indexeddb,Javascript,Indexeddb,为什么我会出错 我的代码: 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.IDBTransac
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”:数据库未运行版本更改事务。index.html:37
idbRequest.onsuccess
不能在
onsuccess
方法中创建objectStore
。您只能在upgradeneeded
事件中执行此操作
引用文件:
创建新数据库或增加现有数据库的版本号时(通过指定比以前打开数据库时更高的版本号),将触发onupgradeneeded
事件。在该事件的处理程序中,应创建此版本数据库所需的对象存储
请参阅。“Щззза”是什么意思我理解您的所有评论,除此之外:-)如果在OnUpgradeRequired中填充数据失败怎么办?那么我是否应该在onsuccess中进行数据验证,如果数据无效,然后使用更高版本打开数据库以再次触发OnUpgradeRequired?