9

In Firefox 17.0.1 when I try to open the IndexedDB database, Firebug console shows me an InvalidStateError exception. Also request.onerror event is raised, but event.target.errorCode is undefined.

if (window.indexedDB) {
    var request = window.indexedDB.open('demo', 1);
    request.onsuccess = function(event) {
        // not raised
    };
    request.onupgradeneeded = function(event) {
        // not raised
    };
    request.onerror = function(event) {
        // raised with InvalidStateError
    };
}

Does anyone have experience with IndexedDB in Firefox?

Update

Firefox 18.0.1 has the same behavior. Comlete source.

Josh
  • 17,834
  • 7
  • 50
  • 68
Václav Dajbych
  • 2,584
  • 2
  • 30
  • 45
  • Hi, I tested it on FF 17.0.1 and it worked for me, also on the 19.0.1. Are you sure nothing else is going wrong? maybe the current version of the db on your browser is higher then 1 and that is why you get the error? Try opening the db in a higher version and see if it works – Kristof Degrave Feb 11 '13 at 15:19

4 Answers4

5

I answer because the problem still exists (in Firefox 54). This happens if you:

To prevent the InvalidStateError a try catch isn't working (but useful for other errors, e.g. disabled cookies), instead you need event.preventDefault(). Yes I know, too easy to be true. :)

if (window.indexedDB) {
    var request = window.indexedDB.open('demo', 1);
    request.onsuccess = function(event) {
        // not raised
    };
    request.onupgradeneeded = function(event) {
        // not raised
    };
    request.onerror = function(event) {
        // raised with no InvalidStateError
        if (request.error && request.error.name === 'InvalidStateError') {
            event.preventDefault();
        }
    };
}

Kudos go to https://bugzilla.mozilla.org/show_bug.cgi?id=1331103#c3.

RiZKiT
  • 2,107
  • 28
  • 23
1

I am pretty sure the error you get is a version error, meaning the current version of the database is higher then the version you are opening the database with. If you take a look in event.target.error you will see that the name will contain "VersionError".

An other possibility is that you will see "AbortError", that would mean that the VERSION_CHANGE transaction was aborted. Meaning there was an error in the onupgradeneeded event that caused an abort. You could get this if you are creating an object store that already exists.

I see no other possibilities than these too, if not provide some more info about the error you get.

Kristof Degrave
  • 4,142
  • 22
  • 32
  • I tried to run the code on another domain and the error is the same. When I debug JS in Firebug, `onupgradeneeded` is not fired. – Václav Dajbych Feb 12 '13 at 14:14
  • 1
    what Kristof said is that you need to open the database without the version property. that's the reason why you get the version error. – Deni Spasovski Feb 12 '13 at 20:02
  • Deni is correct. By calling the open method without a version number, you will open the database in his current version. Once you have done this should be able to check the current version of the database in the success handle with event.target.result.version – Kristof Degrave Feb 13 '13 at 09:27
  • Removing the version number did not help. There is a version number in the Mozila's IndexedDB demo: https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB – Václav Dajbych Feb 15 '13 at 16:24
  • What is the error you got when you didn't provide a version number? Did you take a look in the event.target.error to see the error you get? An other thing you can do is get my wrapper and my viewer. With my viewer you will be able to get the details of your database. On http://linq2indexeddb.codeplex.com you will find my lib and viewer. On my blog http://www.kristofdegrave.be/2012/11/indexeddbviewer-take-look-inside-your.html you will find how to use it. – Kristof Degrave Feb 16 '13 at 06:25
0

You need to create the object store in a separate transaction, you're lumping both the open database and create object store transaction into the same event.

Also you can't have both autoincrement and a path as options to your object store. You have to pick one or the other.

Here's the code that will get your example going:

    function initDB() {
        if (window.indexedDB) {
            var request = window.indexedDB.open('demo', 1);
            request.onsuccess = function(event) {
                db = event.target.result;
                createObjectStore();
            };
            request.onupgradeneeded = function(event) {
                db = event.target.result;
                $('#messages').prepend('blah blah<br/>');
            };
            request.onerror = function(event) {
                $('#messages').prepend('Chyba databáze #' + event.target.errorCode + '<br/>');
            };
        }
    }

    function createObjectStore() {
        db.close();
        var request = window.indexedDB.open('demo', 2);
        request.onsuccess = function(event) {
            db = event.target.result;
            showDB();
        };
        request.onupgradeneeded = function(event) {
            db = event.target.result;
            $('#messages').prepend('yeah yeah yeah<br/>');
            var store = db.createObjectStore('StoreName', { keyPath: 'id' });
            store.createIndex('IndexName', 'id', { unique: true });
        };
        request.onerror = function(event) {
            $('#messages').prepend('Chyba databáze #' + event.target.errorCode + '<br/>');
        };
    }

If you start getting stuck you can take a look at some indexeddb code I wrote for the Firefox addon-sdk. The code is more complicated than what you need but you'll be able to see all the events, errors, and order of transactions that need to happen. https://github.com/clarkbw/indexed-db-storage

Good luck!

Bryan Clark
  • 2,542
  • 1
  • 15
  • 19
0

FireFox will also throw an "InvalidStateError" when using IndexedDB, if the browser is set to "Do not store history" in the privacy tab of the FireFox settings.

I believe FireFox basically runs in incognito mode when that setting is set. IndexedDB is not available when running FireFox in private mode.