簡體   English   中英

NotFoundError:在indexedDB中創建事務時,DOM IDBDatabase異常8

[英]NotFoundError: DOM IDBDatabase Exception 8 when creating transaction in indexedDB

我嘗試將Indexed DB API用於某些測試。

我的代碼如下:

<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>

我有一個Web服務器可以使用具有localhost域的頁面。

當我使用Firefox 22.0加載頁面時,數據庫打開成功。 但是,當我單擊“添加”按鈕並調用addToDB函數時,在Firefox控制台中出現以下錯誤:

NotFoundError:操作失敗,因為找不到請求的數據庫對象。 例如,對象存儲不存在但正在打開。
var tx = db.transaction(store_name,mode);

我也在Chrome 24上進行了相同的測試。當我單擊“添加”按鈕時,錯誤來自同一行。var tx = db.transaction(store_name,mode); 在Chrome控制台上,我出現以下錯誤:

未捕獲的錯誤:NotFoundError:DOM IDBDatabase異常8

通過搜索有關該異常的更多信息,我找到了以下鏈接:

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

在該鏈接中,將其標記為異常8: 例如,通過調用IDBTransaction.abort終止了請求。

因此,我的問題我不知道為什么此時我的請求被中止。

有人有想法來解決這個問題嗎?

謝謝。

西爾

由於執行if (db.version != dbVersion)檢查,因此未創建對象存儲。 該塊將永遠不會輸入。 只需刪除檢查,更改為const dbVersion = 2; 一切都會好起來的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM