InvalidStateError“在不允许突变的数据库上尝试了变异操作。”在Firefox中的indexedDB中

时间:2013-06-18 17:58:55

标签: javascript firefox indexeddb

我有以下代码,它会在 Firefox 中引发InvalidStateError(在调用objectStore时)。 消息为A mutation operation was attempted on a database that did not allow mutations.

奇怪的是,当我将事务的创建放在getBlob回调中时,它似乎有效。

function saveFile( fileEntry ) {
    var transaction = this;
    transaction.onerror = function( event ) {
        console.log( event );
    };

    fileEntry.getBlob( "", function( blob ) {
        var store = transaction.objectStore( STORE_NAME );
        try {
            var request = store.put( blob, fileEntry.getFullname() );
            request.onsuccess = function( event ) {
                console.log( "file: " + fileEntry.getFullname() );
                ++(progressBar[0].value);
            };
            request.onerror = function( event ) {
                console.log( "error in file " + event );
                ++(progressBar[0].value);
            };
        } catch( ex ) {
            console.log("getBlob " + ex );
        }
    }, function() {
    }, true );
}

function saveRecursive( dirEntry, missingFiles ) {
    var db = this;
    var transaction = db.transaction( [STORE_NAME],
                                  MODES.READ_WRITE );

    for( var i in dirEntry.children ) {
        var entry = dirEntry.children[i];

        if( entry.directory ) {
            createDirectory( dirEntry, entry );
            saveRecursive.call( db, entry, missingFiles );
            continue;
        }

        var index = missingFiles.indexOf( entry.getFullname() );
        if( index == -1 )
            continue;

        // Still missing - add
        missingFiles.splice( index, 1 );
        saveFile.call( transaction, entry );
    }
}

有人可以向我解释,为什么这不起作用?

2 个答案:

答案 0 :(得分:1)

如果getBlob是异步函数,则不起作用:

fileEntry.getBlob( "", function( blob ) {
  var store = transaction.objectStore( STORE_NAME );

因为transaction已经提交,当你得到blob时。

答案 1 :(得分:0)

还不确定indexeddb中是否支持blob。