在Chrome

时间:2016-02-08 17:51:18

标签: javascript google-chrome indexeddb

我正在尝试使用IndexedDB,一切都运行良好,直到几个小时前我的.open()回调停止在Chrome上调用(在Safari上测试,它工作正常)。

RT.prototype.setupIndexedDB = function (callback) {
  var dbName = "test";
  var dbVersion = 1;
  var indexedDB = window.indexedDB ||
                  window.webkitIndexedDB ||
                  window.mozIndexedDB;

  console.log("setup indexed db");
  var request = indexedDB.open(dbName, dbVersion);
  request.onsuccess = function(e) {
    console.log("db request success");
  };

  request.onblocked = function(e) {
    console.log("DB open blocked", e);
  };

  request.onerror = function(err) {
    console.log("error", err);
  };

  request.onversionchange = function(err) {
    console.log("onversionchange", err);
  };

  request.onupgradeneeded = function(e) {
    console.log("upgrade needed");
  };
};

我查看了Chrome开发人员工具IndexedDB,没有任何内容......

任何人都知道发生了什么事?

谢谢

编辑:完全重新启动浏览器就可以了,但我仍然对了解发生的事情非常感兴趣。

1 个答案:

答案 0 :(得分:5)

您是否在同一时间尝试删除数据库?如果存在阻塞的挂起deleteDatabase操作,则IndexedDB可能会陷入奇怪的(尽管根据规范是正确的)状态。例如。下面的代码。如果所有其他方法都失败了,您可以检查chrome:// indexeddb-internals,它会告诉您是否有任何待处理的打开或删除操作正在解决问题。

var request = indexedDB.open("test", 1);
request.onblocked = function(e) {
  console.log("DB open blocked", e);
};

request.onerror = function(err) {
  console.log("DB open error", err);
};

request.onupgradeneeded = function(e) {
  console.log("DB open upgrade needed");
};

request.onsuccess = function(e) {
  db = request.result;
  console.log("DB open success");
  db.onversionchange = function(e) {
    console.log("DB got a versionchange event")
  }
  request = indexedDB.deleteDatabase("test");
  request.onsuccess = function(e) {
    console.log("delete success will not be called")
  }
  request.onblocked = function(e) {
    console.log("delete was blocked");
    request = indexedDB.open("test", 1);
    request.onsuccess = function(e) {
      console.log("success won't be called");
    };
    request.onblocked = function(e) {
      console.log("blocked won't be called", e);
    };
    request.onerror = function(err) {
      console.log("error won't be called", err);
    };
    request.onupgradeneeded = function(e) {
      console.log("upgradeneeded won't be called");
    };
  };
};