0

无法理解为什么会这样:

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                                
                            }

当参考数据库“已知”分配 db 时,为什么会弹出错误?

4

2 回答 2

1

您只能在版本更改事务中调用 createObjectStore,这大致对应于 upgradeneeded 事件处理程序。此外,它是“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;                                
}

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

于 2013-08-26T04:02:26.313 回答
0

看起来你忘了给回调命名参数?尝试:

request.onsuccess = function(event) ...

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

于 2013-08-25T13:50:02.150 回答