admin管理员组

文章数量:1277405

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.

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.

Share Improve this question edited May 25, 2014 at 3:02 Josh 18.7k7 gold badges54 silver badges72 bronze badges asked Dec 20, 2012 at 12:36 VaclavDVaclavD 2,6322 gold badges31 silver badges46 bronze badges 1
  • 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 Commented Feb 11, 2013 at 15:19
Add a ment  | 

4 Answers 4

Reset to default 5

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/show_bug.cgi?id=1236557, https://bugzilla.mozilla/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/show_bug.cgi?id=1331103#c3.

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.

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 plicated 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./clarkbw/indexed-db-storage

Good luck!

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.

本文标签: javascriptInvalidStateError while opening IndexedDB in FirefoxStack Overflow