在indexedDB的控制台中发现错误,需要帮助

kx1ctssn  于 2023-03-06  发布在  IndexedDB
关注(0)|答案(1)|浏览(209)

我尝试创建的indexedDB出现错误。“未捕获DOMException:无法对“IDBObjectStore”执行“索引”:指定的索引没有找到。”它说它没有找到,但我相信我正确地创建了索引前几行。
另外,我正在学习,因为我对indexedDB的了解太少了
代码如下

//Credit @alexeagleson - github

// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
const indexedDB =
  window.indexedDB ||
  window.mozIndexedDB ||
  window.webkitIndexedDB ||
  window.msIndexedDB ||
  window.shimIndexedDB;

//window.indexedDB.deleteDatabase("MeteorDynamicImportCache");//
//window.location.reload();//

// Open (or create) the database
const request = indexedDB.open("Workouts", 1);

request.onerror = function (event) {
  console.error("An error occurred with IndexedDB");
  console.error(event);
};

// Create the schema on create and version upgrade
request.onupgradeneeded = function () {
  const db = request.result;
  const store = db.createObjectStore("workouts", { keyPath: "id" });
  store.createIndex("name_Type", ["Type"], { unique: false });
  store.createIndex("specifics", ["Type","Where","For"], {unique: false,});
};

request.onsuccess = function () {
  console.log("Database opened successfully");

  const db = request.result;
  const transaction = db.transaction("workouts", "readwrite");

  const store = transaction.objectStore("workouts");
  const nameIndex = store.index("name_Type"); [tag:ERROR HERE]
  const specificsIndex = store.index("specifics");

我试着改变字母大小写,因为这似乎可以澄清我上次遇到的类似错误。我真的不知道还能做什么。

vybvopom

vybvopom1#

我不知道这是否是您问题的答案,自从我使用indexedDB以来已经有好几年了;但是我回顾了我过去工作的一些代码,注意到db的赋值与您的代码中的不同。当您第一次打开数据库时,onupgradeneeded事件在onsuccess之前触发。显然,我对此也感到困惑,因为我过去对它做过具体的评论。在onupgradeneeded事件中,将局部变量db设置为event.target.result,不设置为req.result,然后在onsuccess事件中,将全局变量db设置为req.result;并且在该事件内,在该X1 M11 N1 X上设置X1 M10 N1 X事件。
我简单地看了一下MDN上的indexedDB文档,他们使用event.target.result
也许这没有什么区别,但可能值得进一步研究。

req = o.version ? indexedDB.open( o.db_name, o.version ) : indexedDB.open( o.db_name );

req.onupgradeneeded = ( event ) => {
  /*
  1. Note that the onupgradeneeded event automatically creates a transaction
     of mode 'versionchange'.
  2. Note that if the onupgradeneeded event "exits successfully", the 
     onsuccess handler of the open database request will then be triggered.
  3. Also, note that best practice is to not write to database in 
     onupgradeneeded, but only in onsuccess event.
  */
  // Save the IDBDatabase interface. 
  let db = event.target.result;
  let editor;
  editor = db.createObjectStore( "edit_1", { keyPath : "key" } );
  editor.createIndex( "nbr", "nbr", { unique: true } );
  /* ... */
}

req.onsuccess = ( event ) => { 
  /* 1. Note that the onsuccess event is fired after the onupgradeneeded
        event completes successfully. 
     2. Apparently can't assign a separate handler to the success or
        failure of onupgradeneeded.
  */
  db = req.result;

  /* The database has a db.version property and the onupgradeneeded event
     has an event.oldVersion and event.newVersion property.
     It would appear that db.version = event.newVersion any time there
     is an upgrade.
  */
  db.onerror = ( event ) => {}
}

相关问题