使用indexedDB时TypeError“versionchange”

时间:2013-06-26 07:14:30

标签: javascript indexeddb

非常感谢Kristof的建议,我更正了我的代码:

var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;

const dbName = "mediasDB";

const db_store = "medias";

var db;


function openDB(dbName, dbVersion) {

    var request = indexedDB.open(dbName, dbVersion);

    request.onupgradeneeded = function (evt) {

        console.log("Upgrade");
        console.log('Creating objectStore');

        var store = evt.currentTarget.result.createObjectStore(db_store);

        store.transaction.oncomplete = function (evt) {

            storeMedias(mediaData, function (media) {
                var store = getObjectStore(db_store, "readwrite")
                console.log(media);
                store.put(media, media.url);
                console.log("Rajout d'un media dans la BD");
            });
        };  
    };

    request.onsuccess = function (event) {
        console.log("openDB DONE");
        db = this.result;       
    };

    request.onerror = function (event) {
        console.log(event.target.result);
    };
}


openDB(dbName, 1);


//On déclenche le défilement des médias au bout de 5 secondes

setInterval(function () {
    $.getJSON("remoteFileJson", function (data) {

         if (data.revision != db.version) {
            console.log('Mise à jour de la BD');
            openDB(dbName, data.revision);

         } else {
            console.log('La base de données est à jour');
         }
    });
}, 10000);

每十秒钟,我得到一个JSON,它给我我的修改数量(它比我检查的db.version更大),但是当我reOpenDB时,没有调用upgradeneeded。

1 个答案:

答案 0 :(得分:1)

您无法手动创建版本更改事务。调用此方法的唯一方法是在打开indexeddb连接时更改版本号。了解更多信息:http://www.kristofdegrave.be/2012/01/indexed-db-defining-your-structure-new.html

var dbRequest = indexeddb.open("name", 3);
dbRequest.onupgradeneeded = function (e)
{
    var versionChangeTrans = e.target.transaction;
}