-2

I have an object containing an array of objects. I am able to save this data into the chrome.storage. Also able to get the data back as an object. But am totally stuck on how to read from this..

Say, I have the following data:

var x = {'data': [{name: '', id: '', url: '', 
'dataquery': [{name: '', url: '', lookfor: '', getback: ''}] }] };

Now say, I save this into chrome storage (using set), and later get this data back as an object 'd' (using get), how do I access, say the id or url of the first array? Shouldn't it be like this:

d.data[0].url // ??

But this gives an error. What am I doing wrong? what is the proper syntax?

EDIT, with more details: Here is the code am using to set and get the data:

// SETTING THE DATA
chrome.storage.sync.set(x, function() {
console.log('STORED'); });

// GETTING THE DATA
var d;
chrome.storage.sync.get('data', function(data) {
    d = data;
    console.log(d);
});

alert(d.data[0].loginurl);

I can see from my console logs that the data has been stored and retrieved properly. I can see the data (as an object) in console.log (shows the array and all elements when you expand).

But the alert line, gives the following error: 'Uncaught TypeError: cannot read property 'data' of undefined'

Mike Hall
  • 11
  • 4
  • That's the correct syntax... And will give you exactly what you want. http://jsfiddle.net/jktyxuu1/1/ – nicael Jun 20 '15 at 19:44
  • You need to show how you get `d` (and probably more). My guess is that you have a duplicate of http://stackoverflow.com/q/23667086/2336725 – Teepeemm Jun 20 '15 at 19:49
  • Alright, editing this question with code on how I am setting & getting the data. But, I can see my data has been stored properly from console.log.. – Mike Hall Jun 20 '15 at 19:50
  • So I guess my syntax is correct, but I need to convert the object I got back from storage into some other format. I tried stringify and it returns 'undefined' (when I try to post the 'stringified' output on page or on console.log).. – Mike Hall Jun 20 '15 at 20:35
  • There seems to be an answer in http://stackoverflow.com/questions/14531102/saving-and-retrieving-from-chrome-storage-sync – Arathi Sreekumar Jun 20 '15 at 20:41
  • @MikeHall Actually, no, you don't see that it's stored & retrieved _in the order it appears on the page_. In all likelihood, that `alert` gets executed before _either_ the `.set()` or `.get()` execute. – Auspex Jun 23 '15 at 01:13
  • Thanks, all of you. But really the question was only about the syntax to access the data inside this object. This was just an example/test.. am not using this code. But it worked during testing.. – Mike Hall Jun 23 '15 at 18:14

2 Answers2

0

Can you give us an excerpt with the relevant code?

I'd guess you should be reading like this:

chrome.storage.sync.get('YOUR_NAME_HERE', function(d) {
  console.log(d);
}

and you'll see what you're getting returned.

geekonaut
  • 5,714
  • 2
  • 28
  • 30
-2

I figured it out. It was a simple error, had declared the variable 'd' outside to make it a global variable. But had not initialized it..

var d; // <- Forgot to initialize the variable
chrome.storage.sync.get('data', function(data) {
    d = data;
    console.log(d);
});

alert(d.data[0].loginurl);

So it was still acting as a local variable, and its value outside of the 'get' function was showing up as 'undefined'.

Mike Hall
  • 11
  • 4
  • 1
    You'll still have the same problem. The `get` function is asynchronous. You can't assume it will have finished by the time you get to the last line of code. In fact, it probably won't have finished. Instead, everything has to happen inside the anonymous `function(data){}`. See the link I provided. – Teepeemm Jun 21 '15 at 10:15