无法初始化属性/方法indexedDB

7xzttuei  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(2)|浏览(224)

无法理解为什么会发生这种情况:

var request=window.indexedDB.open("known");    //async IDB request
request.onsuccess=function(){db=event.target.result;
                             alert("database created"+db);  //it works fine database created
                             var store=db.createObjectStore("friends",{pathKey:"name"})  
                             //error **"Uncaught InvalidStateError: An operation was called on an object on which it is not allowed or at a time when it is not allowed."** as on console box                                
                            }

当数据库已被指定为“已知”数据库时,为什么会弹出错误?

w8biq8rn

w8biq8rn1#

当您在版本变更交易中时,您只能呼叫createObjectStore,这大致Map到需要升级的事件行程常式。而且,它是“keyPath”,而不是“pathKey”。请尝试

var request=window.indexedDB.open("known", 2);    //async IDB request
request.onupgradeneeded = function() {
  console.log("got upgradeneeded event");
  db = event.target.result;
  var store = db.createObjectStore("friends", {keyPath: "name"});
}
request.onsuccess=function(){
  console.log("got success event");
  db=event.target.result;                                
}

规范中有一些很好的例子。

cygmwpex

cygmwpex2#

看起来您好像忘记命名回调的参数了?请尝试:

request.onsuccess = function(event) ...

这样,就定义了“事件”。

相关问题