Answer the question
In order to leave comments, you need to log in
How to deal with IndexedDB?
Demo (Codepen)
Decided to deal with indexedDB. opened the book and began to read. It seems to be more or less clear at first.
I rewrote the code from the book, but this miracle does not want to start.
It feels like the code doesn't seem right at all, at least in terms of syntax...maybe it's out of date. In addition, I did not find the version setting method in the IDBDatabase object. Apparently it is no longer there.
At first the error that db was not found took off. Okay ... I made the variable global, it's not the best practice anymore ...
Then an error began to appear in the console:
Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
let db;
function initiate() {
let databox = document.querySelector("#databox");
let button = document.querySelector("#save");
button.addEventListener("click", addObject, false);
let request = indexedDB.open("mydatabase", 1);
request.addEventListener("error", showerror, false);
request.addEventListener("success", start, false);
}
function showerror(event) {
alert(`Error: ${event.code} ${event.message}`);
}
function start(event) {
console.log("start");
db = event.result || event.target.result;
console.log(db); // IDBDatabase {}
if (db.version == "") {
let request = db.setVersion("1.0");
request.addEventListener("error", showerror, false);
request.addEventListener("success", createDB, false);
}
}
function createDB() {
console.log("createDB");
let objectStore = db.createObjectStore("movies", { keyPath: "id" });
console.log(objectStore);
objectStore.createIndex("SearchYear", "data", { unique: false });
}
function addObject() {
console.log("addObject");
let keyword = document.querySelector("#keyword").value;
let title = document.querySelector("#text").value;
let year = document.querySelector("#year").value;
let transaction = db.transaction(["movies"], IDBTransaction.READ_WRITE);
let objectStore = transaction.objectStore("movies");
let request = objectStore.add({
id: keyword,
name: title,
date: year
});
request.addEventListener("success", () => {
show(keyword);
}, false);
document.querySelector("#keyword").value = "";
document.querySelector("#text").value = "";
document.querySelector("#year").value = "";
}
function show(keyword) {
console.log("show");
let transaction = db.transaction(["movies"]);
let objectStore = transaction.objectStore("movies");
let request = objectStore.get(keyword);
request.addEventListener("success", showList, false);
}
function showList(event) {
console.log("showList");
let result = event.result || event.target.result;
databox.innerHTML = `<div>${result.id} - ${result.bane} - ${result.date}</div>`;
}
window.addEventListener("load", initiate, false);
Answer the question
In order to leave comments, you need to log in
i use localForage library
conveniently and simply
https://localforage.github.io/localForage/
Isn't it on codepen? It seems to work right? Or I misunderstood...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question