属性/方法无法初始化indexedDB

时间:2013-08-25 06:41:18

标签: javascript javascript-events indexeddb

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

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已经分配了对“已知”数据库的引用时,为什么会弹出错误

2 个答案:

答案 0 :(得分:1)

当您处于versionchange事务中时,您只能调用createObjectStore,该rougly对应于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;                                
}

spec中有一些很好的例子。

答案 1 :(得分:0)

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

request.onsuccess = function(event) ...

这样就定义了“事件”。