Access results (in array) of indexedDB request outside -
i trying value of iterated objects of 1 of objectstore using code:
enter codefunction get_object(object){     var object = object;     var obj_list = [];     var transaction = db.transaction([object],"readonly");     var obj_store   = transaction.objectstore(object);      obj_store.opencursor().onsuccess = function (event){         cursor = event.target.result;         if(cursor){             console.log(cursor.value);             obj_list.push(cursor.value);             cursor.continue();         }else{             console.log("done processing " + object + "...");             return obj_list;         }     }; } what want access obj_list outside of said method?
change main function this:
function get_object(object){     return new promise(function(resolve, reject) {         var object = object;         var obj_list = [];         var transaction = db.transaction([object],"readonly");         var obj_store   = transaction.objectstore(object);          obj_store.opencursor().onsuccess = function (event){             cursor = event.target.result;             if(cursor){                 console.log(cursor.value);                 obj_list.push(cursor.value);                 cursor.continue();             }else{                 console.log("done processing " + object + "...");                 resolve(obj_list);             }         };     }); } then access it:
get_object('some_object').then(function (returned_data) {     console.log(returned_data); }); 
Comments
Post a Comment