未捕获的TypeError:使用indexedDB时在Chrome中键入错误 [英] Uncaught TypeError: Type error in Chrome when using indexedDB

查看:190
本文介绍了未捕获的TypeError:使用indexedDB时在Chrome中键入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置indexeddb存储以用于chrome。但是当我尝试设置 READ_WRITE 事务时,我得到未捕获的TypeError

I am trying to set up indexeddb storage for use in chrome. But am getting an Uncaught TypeError when I try to set up a READ_WRITE transaction.

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

I have not been able to find good, up to date info on using webkitIDB. So I'm basically flying blind here. Any ideas what I've done wrong? Are there good tuts out there on this that I missed?

设置:

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 已定义。

The requested objectStore has been created, and confirmed that this.dbTransaction is defined.

推荐答案

这不是从对象存储库抛出的IndexedDB错误,但在安装程序中。将错误的对象类型传递给调用时会抛出此类错误,这就是为什么我的第一个猜测是 objectStore var实际上不是字符串。

This is not an IndexedDB error thrown from the object store, but something in the setup. This kind of error is thrown when you pass the wrong object type to an invocation, which is why my first guess was that the objectStore var was not actually a string.

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

Based on elimination this.db is not undefined (else it would error on transaction), transaction is a function (else it would throw non-function invocation). So I have to guess that this.dbTransaction.READ_WRITE should be returning 1 just fine (double check this).

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

Therefore, I strongly suspect this is your 3rd parameter causing issues. I am fairly certain I have never used the 3rd param shown in spec (optional timeout) and believe it's unnecessary here since the default timeout is already 0 (indefinite). Can you try to change that line to the following and see if it works?


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

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

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

UPDATE: Note that the version constants are now deprecated. Instead of those numeric values, you need to pass a string now: "readwrite", "readonly" or "versionchange".

这篇关于未捕获的TypeError:使用indexedDB时在Chrome中键入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆