title: Uncaught TypeError: Cannot call method 'transaction' of undefined when using indexedDB
body: <p>I have the following code for getting records from indexeddb on chrome 30.</p>
  
  <pre><code>var IndexedDBStorage = function (name) {
  // until we won't need this prefix mess
  var indexedDB = window.indexedDB || window.webkitIndexedDB
      || window.mozIndexedDB || window.msIndexedDB;
  var IDBTransaction = window.IDBTransaction ||
      window.webkitIDBTransaction;
  var db;
  // The initialization of our stuff
  this.Supported = function () {
      return indexedDB;
  };
  this.type = function () {
      return "IndexedDB";
  };
  this.Setup = function () {
      var dbVersion = 1.0;
      var openRequest = indexedDB.open(name, dbVersion);
  
      //handle setup - as the spec like it
      openRequest.onupgradeneeded = function (e) {
          console.log("running onupgradeneeded");
          var thisDb = e.target.result;
          if (!thisDb.objectStoreNames.contains(name)) {
  
              var objectStore = thisDb.createObjectStore(name, {
                  autoIncrement: false
              });
              objectStore.createIndex("dataKey", "dataKey",
                  { unique: false });
          }
      };
  
      openRequest.onsuccess = function (e) {
          db = e.target.result;
          db.onerror = function (event) {
              alert("Database error: " + event.target.errorCode);
              console.dir(event.target);
          };
          if (db.setVersion) {
              console.log("in old setVersion: " + db.setVersion);
              if (db.version != dbVersion) {
                  var req = db.setVersion(dbVersion);
                  req.onsuccess = function () {
                      var ob = db.createObjectStore(name, {
                          autoIncrement: false
                      });
                      ob.createIndex("datakey",
                          "datakey", { unique: false });
                      var trans = req.result;
                      trans.oncomplete = function (ex) {
                          console.log("== trans oncomplete ==");
  
                      };
                  };
              }
          }
          console.log(db);
  
      };
  };
  this.GetAll = function (callback) {
      console.log(db);
      var transaction = db.transaction([name]); &lt;-- gives error described below
      var store = transaction.objectStore(name);
      var items = [];
  
      transaction.oncomplete = function (evt) {
          callback(items);
      };
  
      var cursorRequest = store.openCursor();
      cursorRequest.onerror = function (error) {
          console.log(error);
      };
  
      cursorRequest.onsuccess = function (evt) {
          var cursor = evt.target.result;
          if (cursor) {
              items.push({ key: cursor.key, body: cursor.value.body });
              cursor.continue();
          }
      };
  };
  
  
  };
  </code></pre>
  
  <p>if i call it from a button like this :
  it works fine if i do like this and call it from a button click, it works just fine:</p>
  
  <pre><code> function Init() { &lt;-- called from script tag in index.html
  $(document).ready(function () {
  
      window.dataStore = new Store("data");
  
  });
  }
  
  function getAll() { &lt;-- button lick function
  window.dataStore.getAll();
  }
  </code></pre>
  
  <p>however, if i call it directly after initialization like this</p>
  
  <pre><code>function Init() {
  $(document).ready(function () {
  
      window.dataStore = new Store("data");
  window.dataStore.GetAll();
  
  });
  }
  </code></pre>
  
  <p>i get a error with
  <strong>Uncaught TypeError: Cannot call method 'transaction' of undefined</strong> </p>
  
  <p>i am guessing it is because the <strong>db variable has not yet been globally set from openRequest.onsuccess</strong> when i call directly after init. </p>
  
  <p>How can i fix this so it gets set properly    </p>