1

I'm a real noob to Javascript/JSON, so this might be really obvious. I'm storing values to chrome.storage in a chrome extension JS file:

chrome.storage.sync.set({'username' : username}, function() {
    console.log('Saved',username);
}); 
chrome.storage.sync.set({'password' : password}, function() {
    console.log('Saved', username);
});

I know it's passed correctly because the console.log returns the right values.

Then I try to retrieve it in my content script, but it only returns [object Object].

chrome.storage.sync.get("username", function (username) {
    console.log("Passed successfully: Username "+username);
    studentUsername = username;
});

chrome.storage.sync.get('password', function (password) {
    console.log("Passed successfully: Password "+password);
    studentUsername = username;
});

I'm not sure why it's doing this.

Teepeemm
  • 4,331
  • 5
  • 35
  • 58
The-Arkanian
  • 61
  • 2
  • 5

1 Answers1

6

Yes, it always returns an object. You'll have to access the property from the object.

chrome.storage.sync.get("username", function (obj) {  
    console.log("Passed successfully: Username "+obj.username)
    studentUsername = obj.username; 
});
apscience
  • 7,033
  • 11
  • 55
  • 89
  • 1
    The relevant api: https://developer.chrome.com/extensions/storage#method-StorageArea-get – Teepeemm Nov 26 '15 at 01:23
  • **BIG RED SCARY WARNING:** Assigning the result to a variable within an async callback and hoping it would work [may lead to problems](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). – Xan Nov 26 '15 at 09:52
  • @Xan not the case here thou – Zig Mandel Nov 26 '15 at 13:13
  • No, but someone using this code could use the warning. – Xan Nov 26 '15 at 13:13