IndexedDB: 在 ‘IDBDatabase’ 上执行 ‘transaction’ 失败:未找到指定的对象存储之一

huangapple go评论79阅读模式
英文:

IndexedDB: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found

问题

我刚刚为我的React PWA启动了一个indexedDB,并且遇到了以下错误。

Uncaught DOMException: 'IDBDatabase'上执行'transaction'操作失败找不到指定的对象存储之一位于request.onsuccess

问题出现在这一行:

const transaction = db.transaction("storeName", "readwrite");

我编写的函数如下:

const handleSubmit = (event) => {
  event.preventDefault();

  const request = window.indexedDB.open("NewApp", 1);

  request.onsuccess = () => {
    if (nameArea && enterDate) {
      const db = request.result;
      const transaction = db.transaction(["apps"], "readwrite");
      const store = transaction.objectStore("apps");
      store.put({ nameArea, enterDate: Date.now() });

      const query = store.get(1);

      query.onsuccess = function () {
        console.log("下一个信息", query.result);
      };

      transaction.oncomplete = function () {
        // db.close();
      };
    }
  };

  request.onerror = function (event) {
    console.error("IndexedDB发生错误");
    console.error(event);
  };
};

这个问题只在新打开的浏览器中出现,当我调试时,记录被添加,但不仅仅是空字符串。

英文:

I just start a indexedDB for my react PWA. And i get this error.

Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found. at request.onsuccess

line is : const transaction = db.transaction("storeName", "readwrite");

and the func i did:

  const handleSubmit = (event) => {
    event.preventDefault();

    const request = window.indexedDB.open("NewApp", 1);

    request.onsuccess = () => {
      if (nameArea && enterDate) {
        const db = request.result;
        const transaction = db.transaction(["apps"], "readwrite");
        const store = transaction.objectStore("apps");
        store.put({ nameArea, enterDate: Date.now() });

        const query = store.get(1);

        query.onsuccess = function () {
          console.log("next info", query.result);
        };

        transaction.oncomplete = function () {
          /db.close();
        };
      };

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

That problem is in only new opened browser, when i debug it records are adding but not only empty string.

答案1

得分: 1

这意味着要么对象存储尚未创建,要么您正在使用已过时的数据库连接。如果在另一个标签页中升级了数据库,则需要通过在“versionchange”事件中重新打开数据库来升级连接。

英文:

It means either that the object stores have not yet been created, or you are using an outdated db connection.

If the database has been upgraded in another tab, you need to upgrade the connection by re-opening the database in the versionchange event.

huangapple
  • 本文由 发表于 2023年1月8日 23:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定