1

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!

2 Answers 2

3

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:

var request = indexedDB.open("some db");
console.log(request.error);

whereas

var request = indexedDB.open("some db");
request.onerror = request.onsuccess = function(e) { console.log(request.error); };

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Another cause of "InvalidStateError: DOM IDBDatabase Exception 11" is when you try and carry out another transaction inside a setVersion operation in Chrome - typically populating data right after the database has been created/upgraded.

If you need to populate data after setVersion has completed then you MUST use this pattern otherwise you'll get that exception - strangely too with Chrome 22 I only saw this once an extension had been released to the chrome store.

Make sure you use this pattern:

https://groups.google.com/a/chromium.org/forum/#!msg/chromium-html5/VlWI87JFKMk/6GWbB_HvxtsJ

db.setVersion('3').onsuccess = function(event) {
var transaction = event.result;
transaction.oncomplete = function() {
  db.transaction('mystore', 'readwrite').onsuccess = populateDataStore;
 };
}

2 Comments

Isn't setVersion deprecated now?
Yes. developer.mozilla.org/en-US/docs/IndexedDB/… and code.google.com/p/chromium/issues/detail?id=161114 confirms this. It's a few months back since I used that code. I'm pretty sure this code was supporting the older versions of IndexedDB in browsers before the newer method of opening the database was fully supported across all browsers (IDBDatabase.open() / onupgradeneeded) - as we had clients on older and newer versions of Chrome. If you don't need to support the older versions then ignore setVersion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.