IndexedDB 未捕获NotFoundError

tkqqtvp1  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(165)

我是javascript的新手,目前正在尝试编写一个扩展来跟踪用户访问的所有URL。我正在为一位教授做研究,被告知使用indexedDB来跟踪URL。我一直收到以下错误:
Uncaught NotFoundError: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
下面是我的代码::

let request = window.indexedDB.open("MyTestDatabase", 1),
  db,
  tx,
  store,
  index;

request.onerror = function (e) {
  console.log("The database 'linkStorage' could not be opened");
};

request.onupgradeneeded = function (e) {
  let db = request.result,
    store = db.createObjectStore("webpage", { autoIncrement: true }),
    index = store.createIndex("link no.", "webpage", { unique: false });
};

request.onsuccess = function (e) {
  db = request.result;

  tx = db.transaction("webpage", "readwrite");
  store = tx.objectStore("URL");
  index = store.index("link no.");

  db.onerror = function (e) {
    console.log("ERROR" + e.target.errorCode);
  };

  var link = location.href;
  store.put(link);
  tx.oncomplete = function () {
    db.close();
  };
};

有人能帮忙吗?

flmtquvp

flmtquvp1#

您可能在未运行onupgradeneeded函数的情况下创建了数据库。它从未创建对象存储。请尝试增加版本并再次打开数据库。如果存储仍然不存在,则说明您没有正确挂接upgradeneeded事件。

相关问题