簡體   English   中英

Uncaught TypeError:使用indexedDB時,Chrome中出現類型錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM