javascript - chrome.storage.local.get results in "Undefined" when called -
i'm building chrome extension, , needed save data locally; used storage api . got run simple example , save data, when integrated application, couldn't find data , giving me "undefined" result.
here code:
function saveresults(newsid, resultsarray) { //save result for(var = 0; < resultsarray.length; i++) { id = newsid.tostring() + '-' + i.tostring(); chrome.storage.local.set({ id : resultsarray[i] }); } //read , delete saved results for(var = 0; < resultsarray.length; i++) { id = newsid.tostring() + '-' + i.tostring(); chrome.storage.local.get(id, function(value){ alert(value.id); }); chrome.storage.local.remove(id); } }
i not type of data saving or how much, seems me there may more 1 newsid
, resultsarray
of varying length each one. instead of creating keys each element of resultsararry
have considered storing entire thing is. example of be:
chrome.storage.local.set({'results':[]}); function saveresults(newsid, resultsarray) { // first combine data 1 object var result = {'newsid':newsid, 'resultsarray':resultsarray}; // next push each individual results object array chrome.storage.get('results',function(item){ item.results.push(result); chrome.storage.set({'results':item.results}); }); } function getresults(newsid){ chrome.storage.get('results', function(item){ item.results.foreach(function(v,i,a){ if(v.newsid == newsid){ // here v.resultsarray array stored // can remove part of such v.resultsarray.splice(0,1); // or a.splice(i,1); // remove whole object, set again chrome.storage.local.set({'results':a}); } }); }); }
this way don't need worry dynamically naming fields or keys.
Comments
Post a Comment