簡體   English   中英

為什么 db.transaction 不能與 indexeddb 一起使用?

[英]Why is db.transaction not working with indexeddb?

我是使用 inxededdb 的新手,正在嘗試從商店中獲取數據。 存儲包含數據,但由於某種原因,代碼在嘗試設置 var tx 后停止。 如果我遺漏了什么,請告訴我。 這是我試圖獲取這本書的 function:

function getBook(){
    var tx = db.transaction("book", "readonly");
    var store = tx.objectStore("book");
    var index = store.index("by_getid");

    var request = index.get("<?php echo $_GET['book'] ?>");
    request.onsuccess = function() {
      var matching = request.result;
      if (matching !== undefined) {
         document.getElementById("text-container").innerHTML = matching.text;
      } else {
        alert('no match');
        report(null);
      }
    };
}

已解決的版本:

function getBook(){
  var db;
  var request = indexedDB.open("library", 1);  

  request.onsuccess = function (evt) {
    db = request.result; 
    var transaction = db.transaction(["book"]);
    var objectStore = transaction.objectStore("book");
    var requesttrans = objectStore.get(<?php echo $_GET['book'] ?>);

    requesttrans.onerror = function(event) {

    };

    requesttrans.onsuccess = function(event) {
      alert(requesttrans.result.text);
    };      
  };
}

問題可能是您的 db 變量。 您可能正在訪問連接對象的關閉或空實例。

嘗試在函數內部創建數據庫連接。 不要使用全局數據庫變量。

index.get產生主鍵。 您必須使用生成的主鍵獲取記錄值。

我有交易問題,它返回錯誤db.transaction is not a function或返回undefined 你會這樣嘗試,它對我有用:

const table = transaction.objectStore('list');
const query = table.getAll();
query.onsuccess = () => {
  const list = query?.result;
  console.log(list);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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