1

我正在尝试设置 indexeddb 存储以在 chrome 中使用。但是Uncaught TypeError当我尝试建立READ_WRITE交易时,我得到了一个。

我无法找到有关使用 webkitIDB 的最新信息。所以我在这里基本上是盲目的。任何想法我做错了什么?我错过了这方面的好消息吗?

设置:

function OfflineStorage() {
  this.saveJSONString = __bind(this.saveJSONString, this);
  var request,
    _this = this;
  this.dbkeyRange = window.webkitIDBKeyRange;
  this.dbTransaction = window.webkitIDBTransaction;
  this.db = window.webkitIndexedDB;
  request = this.db.open("lucidFrog");
  request.onsuccess = function(e) {
    _this.db = e.target.result;
    return _this.setupDB(); //setupDB() ensures the objectStores have been created.
  };
}    

保存功能:

OfflineStorage.prototype.saveJSONString = function(objectStore, json_string, obj_id) {
  var request, store, transaction;

  //PROBLEM AREA, gives "Uncaught TypeError: Type error"
  transaction = this.db.transaction([objectStore], this.dbTransaction.READ_WRITE, 0);
  ////////////////////

  store = transaction.objectStore(objectStore);
  request = store.put({
    "json": json_string,
    "id": obj_id
  });
  request.onsuccess = function(e) {
    return console.log("YYYYYYEEEEEAAAAAHHHHHH!!!");
  };
};

已创建请求objectStore,并确认this.dbTransaction已定义。

4

1 回答 1

5

这不是从对象存储中抛出的 IndexedDB 错误,而是设置中的某些内容。当您将错误的对象类型传递给调用时会引发这种错误,这就是为什么我的第一个猜测是objectStorevar 实际上不是字符串。

基于消除 this.db 不是未定义的(否则它会在事务中出错),事务是一个函数(否则它会抛出非函数调用)。所以我不得不猜测 this.dbTransaction.READ_WRITE 应该返回 1 就好了(仔细检查一下)。

因此,我强烈怀疑这是您的第三个参数导致问题。我相当肯定我从未使用过规范中显示的第三个参数(可选超时),并且认为这里没有必要,因为默认超时已经是 0(无限期)。您可以尝试将该行更改为以下内容,看看它是否有效?

事务 = this.db.transaction([objectStore], this.dbTransaction.READ_WRITE);

更新:请注意,现在不推荐使用版本常量。您现在需要传递一个字符串,而不是那些数值:“readwrite”、“readonly”或“versionchange”。

于 2012-03-20T22:22:59.230 回答