0

I want to access the variable data mentioned in the code below, outside the idQuery block. I am getting data = {} outside that block.

  const indexedDB = window.indexedDB;
  if (!indexedDB) {
    console.log("IndexedDB could not be found in this browser.");
  }

  const request = indexedDB.open("ResumeDatabase", 2);
  request.onerror = function (event) {
    console.error("An error occurred with IndexedDB");
    console.error(event);
  };

  request.onupgradeneeded = function () {
    const db = request.result;
    const store = db.createObjectStore("resume", { keyPath: "id" });
  };

  const onChange = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
    setValue(event.target.value);

    let oldData = {};
    let data = {};
    request.onsuccess = function (event) {
      console.log("Database opened successfully");
      const db = request.result;
      const transaction = db.transaction("resume", "readwrite");
      const store = transaction.objectStore("resume");
      const key = JSON.parse(JSON.stringify({ path })).path.split(".").at(-1);
      const value = (event.target as HTMLInputElement | HTMLTextAreaElement).value;
      const idQuery = store.get(1);
      
      idQuery.onsuccess = () => {
        oldData = idQuery.result;
        
        data = {
          ...oldData,
          [key]:  value,
        }

      };

      store.put({ id: 1, data });
    }
    request.onsuccess(event as any);
  };
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39
Suyog
  • 1
  • 2
  • I have no information about indexedDB, but I think the promises in the code, I hope this tutorial help you https://dev.to/andyhaskell/using-promises-in-indexeddb-4nc0 – Mohammad Yaser Ahmadi Sep 13 '22 at 12:53

0 Answers0