【问题标题】:InvalidStateError "A mutation operation was attempted on a database that did not allow mutations." in indexedDB in FirefoxInvalidStateError “试图对不允许突变的数据库进行突变操作。”在 Firefox 中的 indexedDB 中
【发布时间】:2013-06-15 01:44:47
【问题描述】:

我有以下代码,它会在 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 );
    }
}

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

【问题讨论】:

    标签: javascript firefox indexeddb


    【解决方案1】:

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

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

    因为transaction 已经提交,当您获得 blob 时。

    【讨论】:

    • 事务在 indexedDB API 中自动提交,它们不是为了保持打开状态而构建的,而是在它们处于非活动状态时自动提交
    【解决方案2】:

    不确定 indexeddb 是否支持 blob。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2011-03-01
      相关资源
      最近更新 更多