javascript - 使用 IndexedDB 创建多个对象存储时,Safari 7.1 中出现 UnknownError

标签 javascript safari indexeddb

我想进行健全性检查,看看是否有其他人在使用 Safari 7.1 和 IndexedDB 时遇到问题。似乎我收到了 UnknownError 类型的错误,根据 http://www.w3.org/TR/IndexedDB/ 的规范当“操作因与数据库本身无关且未被任何其他错误涵盖的原因而失败”时发生。在第一次调用调用回调(onSuccess 或 onError)之后,我第二次调用此函数时会发生这种情况。这是我创建对象存储的函数,它适用于 Chrome 和 Firefox。

IndexedDBClient.prototype.createObjectStore = function(options) {
  if (this.checkIfObjectStoreExists(options.objectStoreName)) {
    options.onError(this.objectStoreDNEMessage);
    return;
  }

  var objectStore;

  var objectStoreCreated = false;
  var databaseOpened = false;

  var version = this.database.version;
  var dbName = this.database.name;
  this.database.close();
  var request = indexedDB.open(dbName, ++version);
  var that = this;

  request.onupgradeneeded = function(e) {
    that.database = e.target.result;
    objectStore = that.database.createObjectStore(options.objectStoreName, { keyPath: options.keyPathName });

    objectStore.transaction.oncomplete = function(e) {
      objectStoreCreated = true;
      successCallback();
    }

    objectStore.transaction.onerror = function(e) {
      options.onError(e);
    };
  };

  request.onsuccess = function(e) {
    databaseOpened = true;
    successCallback();
  }

  request.onerror = function(e) {
    options.onError(e);
  };

  request.onblocked = function(e) {
    typeof options.onBlocked === 'function' && options.onBlocked();
  };

  function successCallback() {
    // This is needed because we must be sure that both the objectstore creation transaction has completed,
    // and the db open request has fired the onsuccess event.
    objectStoreCreated && databaseOpened && options.onSuccess(objectStore);
  }
};

最佳答案

Safari 浏览器中的 IndexedDB API 有 bug多个商店交易。

关于javascript - 使用 IndexedDB 创建多个对象存储时,Safari 7.1 中出现 UnknownError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26899035/

相关文章:

JavaScript 变量未定义

javascript - indexedDB openCursor 事务成功返回空数组

javascript - 使用变量设置 <Image/> 的源

javascript - 在 jQuery 中将百分比值设置为scrollLeft()

javascript - 在不影响正常屏幕尺寸的情况下为屏幕尺寸调用样式表

Javascript 正则表达式匹配以字母或数字开头的字符串

ios - .class :hover . otherClass {...} - iOS Safari 不工作

java - 单击事件在使用 Selenium 和 Java 的 MAC OS Safari 13.05 中不起作用

indexeddb - 获取非唯一索引indexedDb的不同值

javascript - 在 IE 10 中使用复合键创建索引时出现 DataError