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);
};