2013-08-25 62 views
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         
          } 

當數據庫已被分配參照數據庫「已知」,那麼爲什麼錯誤彈出

回答

1

當您處於版本更改事務中時,只能調用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中有一些很好的例子。

0

您似乎忘記命名回調的參數?試試:

request.onsuccess = function(event) ... 

這樣就定義了「事件」。