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;
}
1
  • 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 Commented Dec 16, 2019 at 21:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.