Skip to content Skip to sidebar Skip to footer

How Do I Get The Results Of An IndexedDb Request Outside The Scope Of Its Callback

I have a form with a input box that I want to auto-complete with values from a IndexedDb objectStore, this works with two over positioned input boxes. I works fine with a simple ar

Solution 1:

If you want to use it outside the transaction scope, just add it to a variable

var results = [];
var openDbRequest = indexedDB.open(DB_NAME);
openDbRequest.onsuccess = function (e) {
    var db = e.target.result;
    var tran2 = db.transaction("store");
    tran2.objectStore("store").openCursor().onsuccess = function(e) {
         var cursor = e.target.result;
         if (cursor) {
              results.push(cursor.value) 
              cursor.continue();
         };
    };
}; 

After this you van iterate over the results object, and the transaction scope will close once all data is retrieved.


Post a Comment for "How Do I Get The Results Of An IndexedDb Request Outside The Scope Of Its Callback"