属性/方法无法初始化indexedDB

Properties/methods cant be initialised indexedDB

本文关键字:初始化 indexedDB 方法 属性      更新时间:2023-09-26

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

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                                
                            }

当数据库已经参照数据库"被分配时;已知的";那么为什么会出现错误呢?

您只能在版本更改事务中调用createObjectStore,该事务与升级的事件处理程序相对应。此外,它是"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;                                
}

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

似乎忘记为回调命名参数了?尝试:

request.onsuccess = function(event) ...

通过这种方式,定义了"事件"。