I have problem with indexeddb – I’m using this method to open and setup db
DBService.prototype.open = function (dbName, entity, cb) {
var self = this;
var req = indexedDB.open(dbName)
req.onsuccess = function(e) {
var v = 1;
DBService.storage.db = e.target.result;
var db = DBService.storage.db;
// We can only create Object stores in a setVersion transaction;
if (v != db.version) {
var setVrequest = db.setVersion(v);
setVrequest.onerror = function (err, stack) {
console.log(err, stack);
}
setVrequest.onsuccess = function(e) {
if(db.objectStoreNames.contains(entity)) {
db.deleteObjectStore(entity);
}
var store = db.createObjectStore(entity,
{keyPath: "timeStamp"});
e.target.transaction.oncomplete = function() {
console.log(db);
cb(db);
};
};
} else {
cb(db);
/*
req.transaction.oncomplete = function() {
cb(db);
*/
}
};
req.onerror = function (err, stack) {
console.log(err)
console.log(stack)
}
};
Everything works nice except case when db is not ever opened. Im still getting
InvalidStateError: DOM IDBDatabase Exception 11
(when I console.log req.error)
I know this code is pretty dirty now, I’m trying everything. I promise that when it will works well, then I will refactor it!
Thank you!
I’m assuming you are only using this page in chrome since the other browsers no longer support setVersion.
What exactly do you mean “Everything works nice except case when db is not ever opened.”? Sometimes the db.open call fails and req.onerror is called? In any case your error event handler should only take one parameter, only an error is passed, not a stack.
One common cause of the InvalidStateError: DOM IDBDatabase Exception 11 exception you’re getting is accessing req.error before the request object has received any events. For example, this code will throw Exception 11:
whereas
will not throw an exception.
You might also want to add
setVrequest.onblocked = function(e) { console.log("got blocked:" + e); };just so that you know if blocked events could be causing you some trouble.