未捕获的TypeError:使用indexedDB时在Chrome中键入错误

Lee*_*lla 1 javascript indexeddb

我正在尝试设置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.
  };
}    
Run Code Online (Sandbox Code Playgroud)

保存功能:

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!!!");
  };
};
Run Code Online (Sandbox Code Playgroud)

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

bul*_*ley 5

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

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

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

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

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