1

下面是我打开 indexedDB 的 Javascript 代码。我之前在 Firefox 21 中成功测试过代码几次,但现在我看到 e.target.error.name 中的 indexedDB.open() 函数返回了 AbortError。

var openDB = function(dbCallBack) {
  var openDB = function(dbCallBack) {
    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; 
    window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
    window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;

    request = window.indexedDB.open('mgDB');
    request.onerror = function(e) {
      alert('Error: ' + e.target.error.name + ': Failed to open the database');
      dbCallBack(false, false);
    };

    request.onupgradeneeded = function(e) {
      dbCallBack(true, false);
    };

    request.onsuccess = function(e) {
      db = e.target.result;
      if (db.objectStoreNames.length == 0) { 
        dbCallBack(true, true);
      } else {
        dbCallBack(true, false);
      }
    };
  };
};

下面是调用 openDB() 函数的方式。dbCallBack 函数的代码如下内联:

if (!db) {
    var dbOpenSuccess;
    openDB(function(dbOpenSuccess, emptyTableMsg) {
      if (emptyTableMsg) {
        displayEmptyTableMsg();
      } else if (dbOpenSuccess) {
        displayTableContents();         
      }
    }); 
}

注意:在重新运行测试之前,我已尝试清除浏览器缓存并从文件夹 C:\users{userID}\AppData\Roaming\Mozilla\Firefox\Profiles 手动删除 indexedDB。我在http://nparashuram.com/IndexedDB成功运行了相同的代码。有什么问题?

4

1 回答 1

0

如果你听 onabort 事件,你的代码会很好,如下所示。

request.onsuccess = function(e) {
  db = e.target.result;
  db.onabort = function(e) {
    db.close();
    db = null;
  }
  if (db.objectStoreNames.length == 0) { 
    dbCallBack(true, true);
  } else {
    dbCallBack(true, false);
  }
};
于 2013-06-28T07:50:48.240 回答