[英]Uncaught TypeError: Type error in Chrome when using indexedDB
我正在尝试设置供Chrome使用的indexeddb存储。 但是,当我尝试设置READ_WRITE
事务时遇到了Uncaught TypeError
。
我无法找到有关使用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
。
这不是从对象存储引发的IndexedDB错误,而是设置中的错误。 当您将错误的对象类型传递给调用时,会引发此类错误,这就是为什么我的第一个猜测是objectStore
var实际上不是字符串。
基于消除,this.db不是未定义的(否则它将在事务中出错),transaction是一个函数(否则它将引发非函数调用)。 所以我不得不猜测this.dbTransaction.READ_WRITE应该返回1就好了(仔细检查一下)。
因此,我强烈怀疑这是引起问题的第三个参数。 我可以肯定我从未使用过spec中显示的第三个参数(可选的timeout ),并认为这里没有必要,因为默认的timeout已经为0(不确定)。 您可以尝试将该行更改为以下内容,看看是否可行吗?
transaction = this.db.transaction([objectStore],this.dbTransaction.READ_WRITE);
更新:请注意,现在不建议使用版本常量。 代替这些数值,您现在需要传递一个字符串:“ readwrite”,“ readonly”或“ versionchange”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.