【问题标题】:Getting error in indexeddbshim: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable在 indexeddbshim 中出现错误:DOM 异常 11:尝试使用不可用或不再可用的对象
【发布时间】:2015-09-18 06:28:00
【问题描述】:

我正在为 iOS 7.1 使用 IndexedDBShim polyfill(所以底层是 WebSQL),当我尝试运行它时,我得到:

"InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable."

代码:

//Create and open the database
var request = indexedDB.open( "Videos", 1.1 );
var database;

request.onerror = function (event)
{
    console.log( "Unable to create storage for offline videos, an error occurred." );
    console.dir( event );
};

//On success we'll grab the database and store or load the videos
request.onsuccess = function (event)
{
    //Grab the database
    database = request.result;

    //Handle database error
    database.onerror = function (event)
    {
        console.log( "Unable to access storage, an error occurred." );
        console.dir( event );
    };

    downloadVideo();
}

//Create the database and object store
request.onupgradeneeded = function (event)
{
    //Create the video object store (event.target.result is the database)
    event.target.result.createObjectStore( "Videos" );
};

function downloadVideo()
{
    var blob;

    //Start the request
    var videoRequest = new XMLHttpRequest();

    //Get the Video file from the server.
    videoRequest.open( "GET", "videos/test.mp4", true );

    //It's a blob (for storing in database)
    videoRequest.responseType = "blob";

    //Listen for when it's done downloading the video data
    videoRequest.addEventListener(
        "load",
        function ()
        {
            //We got it
            if ( videoRequest.status === 200 )
            {
                //Get the data
                blob = videoRequest.response;

                //Start transaction for videos object store
                var transaction = database.transaction( [ "Videos" ], "readwrite" );

                //Store the video file
                var putRequest = transaction.objectStore( "Videos" ).put( blob, "savedVideo" );

                putRequest.onsuccess = function(e)
                {
                    console.log( "succes!" );
                    console.dir(e);
                }
            }

            //An error occurred
            else console.log( null, "Unable to save, as an error occurred." );
        },
        false
    );

    //Start the request
    videoRequest.send();
}

【问题讨论】:

    标签: indexeddb web-sql indexeddbshim


    【解决方案1】:

    我的第一个猜测是您遇到此错误是因为您使用 database 变量的方式。尝试从加载回调的主体中设置和打开连接,然后在连接打开回调的主体中执行与数据库相关的工作。

    类似于以下缩写代码:

    function downloadVideo(path) {
      console.log('Requesting video blob at %s', path);
      var vr = new XMLHttpRequest();
      vr.onload = onVideoDownload.bind(vr);
      vr.responseType = 'blob';
      vr.open('GET', path);
      vr.send();
    }
    
    function onVideoDownload(request) {
      console.log('Downloaded video blob, connecting to indexedDB');
      var video = request.response;
      var openRequest = indexedDB.open('Videos', 1);
      openRequest.onupgradeneeded = onUpgradeNeeded;
      openRequest.onsuccess = storeVideo.bind(openRequest, video);
    }
    
    function onUpgradeNeeded(event) {
      var database = event.target;
      database.createObjectStore('Videos');
    }
    
    function storeVideo(video, event) {
      console.log('Connected to indexedDB, storing a video');
      var database = event.target.result;
      var tx = database.transaction('Videos','readwrite');
      var videoStore = tx.objectStore('Videos');
      var putRequest = videoStore.put('blob', video);
      putRequest.onsuccess = onStoreVideo.bind(putRequest, video);
    }
    
    function onStoreVideo(video, event) {
      console.log('Successfully stored a video!');
    }
    
    downloadVideo('videos/test.mp4');
    

    【讨论】:

    • 感谢您的回答并抽出宝贵时间提供帮助,非常感谢!我发现了问题 - 它试图将 blob 插入 WebSQL。
    【解决方案2】:

    我试图将一个 blob 插入到数据库中,而没有先用 base64 对其进行编码。就像在 iOS safari 7.1 上一样,支持数据库是 WebSQL,它不接受二进制数据。

    【讨论】:

    • FYR,IndexedDBShim 的 3.x 版本应该会自动为您处理 blob 转换(不过,如果您有任何问题,请提出问题——FWIW,目前有一些失败的 W3C 测试需要处理大斑点)...(我是维护者之一。)还要注意,3.x 将破坏存储在早期版本中的数据。
    猜你喜欢
    • 2015-02-24
    • 2016-04-30
    • 2019-03-05
    • 2022-08-18
    • 2014-05-21
    • 2014-05-19
    • 2013-09-24
    • 2013-12-13
    • 2013-10-08
    相关资源
    最近更新 更多