首页 > 解决方案 > TypeError:indexedDB.databases 不是函数

问题描述

此脚本打印 IndexedDB 的内容。

当我在网站控制台中运行此脚本时,它会显示:

错误:

我尝试使用 IDBDatabase(),但它说“非法构造函数”。

你能指出我的错误吗?

这是代码:

const dumpIndexedDB = (dbName) => {
  const DB_VERSION = 1;
  const req = indexedDB.open(dbName, DB_VERSION);
  req.onsuccess = function () {
    const db = req.result;
    const objectStoreNames = db.objectStoreNames || [];

    console.log(`[*] Database: ${dbName}`);

    Array.from(objectStoreNames).forEach((storeName) => {
      const txn = db.transaction(storeName, "readonly");
      const objectStore = txn.objectStore(storeName);

      console.log(`\t[+] ObjectStore: ${storeName}`);

      // Print all entries in objectStore with name `storeName`

      objectStore.getAll().onsuccess = (event) => {
        const items = event.target.result || [];
        items.forEach((item) => console.log(`\t\t[-] `, item));
      };
    });
  };
};

indexedDB
  .databases()
  .then((dbs) => dbs.forEach((db) => dumpIndexedDB(db.name)));

标签: javascriptindexeddb

解决方案


推荐阅读