Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is db.transaction not working with indexeddb?

I am new at using inxededdb and am trying to get data out of a store. The store contains data, but for some reason the code stops after trying to set the var tx. If I am missing anything please let me know. Here is the function with which I am trying to get the book:

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

Solved Version:

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);
    };      
  };
}
like image 316
wayzz Avatar asked Mar 03 '14 13:03

wayzz


1 Answers

The problem is probably your db variable. You are probably accessing a closed or null instance of a connection object.

Try instead to create the db connection right inside the function. Do NOT use a global db variable.

like image 118
Josh Avatar answered Oct 27 '22 13:10

Josh