I worked a bit with IndexedDB, and I can successfully create a new database, create a repository, and add a value during the “update needed”. What I don’t understand is the database remains open, or do you need to re-open it inside every function that needs read / write access to the information in the database?
For example, here is my code (which works) to create a new database and insert one record:
$(document).ready(function() {
var db;
function initDB() {
console.log("opening db...");
var req = indexedDB.open(dbName, version);
req.onsuccess = function(event) {
db = this.result;
console.log("opening db is done");
}
req.onerror = function(event) {
console.log("error opening db: ", event.target.errorCode);
}
req.onupgradeneeded = function(event) {
db = event.target.result;
var store = db.createObjectStore("creds", { autoincrement: true });
store.add({ username:'none', password:'none'}, 1);
}
}
Which causes me confusion, when I need to access records in this database or add more records or delete records, I thought I could just create a new function and insert some values. This is what I have (which fails):
function saveCreds() {
usr = $("#username").val();
psw = $("#password").val();
$.getJSON(baseURL + "cred-check/?callback=?", { username:usr, password:psw }, function(data) {
if (data.creds_good == 'true'); {
var store = db.transaction(['creds'], 'readwrite').objectStore('creds');
var request = store.get(1);
request.onsuccess = function (event) {
var data = request.result;
data.username = usr;
data.password = psw;
var requestUpdate = store.put(data, 1);
requestUpdate.onerror = function(event) {
console.log("error putting value...");
}
}
}
});
}
saveCreds initDB $(document).ready(), db initDB saveCreds, $(document).ready(), , .
, db undefined. : Cannot call method "transaction" of undefined.
, , , , / ?