Uncaught TypeError: Type error in Chrome when using indexedDB

狂风中的少年 提交于 2019-12-08 04:46:46

问题


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.

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?

Setup:

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.
  };
}    

Save Function:

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!!!");
  };
};

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


回答1:


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.

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).

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);

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".



来源:https://stackoverflow.com/questions/9738364/uncaught-typeerror-type-error-in-chrome-when-using-indexeddb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!