1

In my case, I am creating an offline web app. And want to store the API response in the IndexDB.

In a particular page, I have three to four APIs hitting the server, and I want to store all of the response in Index DB at the same time.

But I am facing a problem in the version number.

I am using - https://github.com/jakearchibald/idb

Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found. I am calling this function for every API.

How to solve this problem?

async updateData(responseData, tableName, keyPath) {
  if (!responseData || !tableName || !keyPath) {
    return;
  }

  const db = await openDB('vnoit', 1, {
    async upgrade(db) {
      if (!db.objectStoreNames.contains(tableName)) {
        db.createObjectStore(tableName, {
          keyPath,
          autoIncrement: true
        });
      }
    }
  });

  if (db.objectStoreNames.contains(tableName)) {
    await db.clear(tableName);
  }

  if (!Array.isArray(responseData)) {
    await db.add(tableName, responseData);
    return;
  }

  const tx = db.transaction(tableName, 'readwrite');

  for (const item of responseData) {
    tx.store.add(item);
  }
  await tx.done;
}
VnoitKumar
  • 1,350
  • 15
  • 29
  • 2
    the error means the object store does not exist so you need to create it first. create all your object stores in the onupgradeneeded handler – Josh Dec 16 '19 at 21:15

0 Answers0