Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidStateError while opening IndexedDB in Firefox

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.

like image 946
Václav Dajbych Avatar asked Dec 20 '12 12:12

Václav Dajbych


People also ask

How do I enable IndexedDB in Firefox?

Double-click on the name dom. indexedDB. enabled to toggle its value.

Does IndexedDB work in incognito mode?

IndexedDB works fine in Chrome incognito mode, so if you have a problem there, it might be caused my something else.


2 Answers

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

  • use Firefox in private mode
  • or switch between different Firefox versions (https://bugzilla.mozilla.org/show_bug.cgi?id=1236557, https://bugzilla.mozilla.org/show_bug.cgi?id=1331103)

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.

like image 77
RiZKiT Avatar answered Oct 27 '22 01:10

RiZKiT


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.

like image 27
Kristof Degrave Avatar answered Oct 26 '22 23:10

Kristof Degrave