简体   繁体   中英

NotFoundError: DOM IDBDatabase Exception 8 when creating transaction in indexedDB

I try to use Indexed DB API for some tests.

My code is the following :

<html>
<head>
    <script type="text/javascript">

        var db = null;
        const dbName = "contactsDB";
        const dbVersion = 1;
        const storeName = "contacts";

        const contacts = [
            {id : 1, firstname : 'F1', lastname : 'L1'},
            {id : 2, firstname : 'F2', lastname : 'L2'}
        ];

        function init() {
            window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
            openDB();
        }

        function openDB() {
            var request = window.indexedDB.open(dbName, dbVersion);

            request.onerror = function (e) {
                alert('DB connexion error : ' + e.target.errorCode);
            };

            request.onsuccess = function (e) {
                alert('DB connexion success');
                // get db instance
                db = e.target.result;
            };

            // seulement implemente sur les browsers recents
            request.onupgradeneeded = function (e) {
                // updgrade DB
                var db = e.target.result;

                if (db.version != dbVersion) {              
                    // create object store
                    var objectStore = db.createObjectStore(storeName, {keyPath : "id"});

                    // create index to search contacts by lastname.
                    // Duplicates possible ==> so no unique index
                    objectStore.createIndex("lastname", "lastname", {unique : false});
                }
            };
        }

        function addToDB() {
            // get object store in tx
            var objectStore = getObjectStore(storeName, "readwrite");

            // stores values
            for (var c in contacts) {
                var request = objectStore.add(contacts[c]);
                request.onsuccess = function (e) {
                    alert('Add success for ' + e.target.result);
                }
            }

        }

        function getObjectStore(store_name, mode) {
            var tx = db.transaction(store_name, mode);
            return tx.objectStore(store_name);
        }

    </script>
</head>
<body onload="init();">
    <input type="button" onclick="addToDB();" value="Add" />
</body>
</html>

I have a web server to use pages with localhost domain.

When I load the page with Firefox 22.0, the DB open succeed. However, when I click on the Add button and so addToDB function is called I have the following error in Firefox console :

NotFoundError: The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened.
var tx = db.transaction(store_name, mode);

I also made the same test on Chrome 24. When, I click on the Add button the error is from the same line var tx = db.transaction(store_name, mode); and on Chrome console I have the following error :

Uncaught Error: NotFoundError: DOM IDBDatabase Exception 8

By searching more info about that exception, i found the following link :

https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabaseException?redirectlocale=en-US&redirectslug=IndexedDB%2FIDBDatabaseException

In that link, it's marked for Exception 8 : A request was aborted, for example, through a call to IDBTransaction.abort.

So, my problem I don't know why my request is aborted at that point.

Someone have an idea to help to fix that problem ?

Thanks.

Sylvain

Your object store isn't being created because of the if (db.version != dbVersion) check. That block will never be entered. Just remove the check, change to const dbVersion = 2; and all should be well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM