javascript 无法在“IDBDatabase”上执行“createObjectStore”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24382085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to execute 'createObjectStore' on 'IDBDatabase'
提问by Michael Phelps
why do I have a error ?
为什么我有错误?
My code:
我的代码:
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
};
Uncaught InvalidStateError: Failed to execute 'createObjectStore' on 'IDBDatabase': The database is not running a version change transaction. index.html:37 idbRequest.onsuccess
未捕获的 InvalidStateError:无法在“IDBDatabase”上执行“createObjectStore”:数据库未运行版本更改事务。index.html:37 idbRequest.onsuccess
回答by Dick van den Brink
You can't create an objectStorein an onsuccessmethod. You can only do this in a upgradeneededevent.
您不能objectStore在onsuccess方法中创建一个。您只能在upgradeneeded事件中执行此操作。
Quote from docs:
来自文档的引用:
When you create a new database or increase the version number of an existing database (by specifying a higher version number than you did previously, when Opening a database), the
onupgradeneededevent will be triggered. In the handler for this event, you should create the object stores needed for this version of the database
当您创建新数据库或增加现有数据库的版本号时(通过指定比以前更高的版本号,在打开数据库时),
onupgradeneeded将触发该事件。在此事件的处理程序中,您应该创建此版本数据库所需的对象存储
See documentation.
请参阅文档。

