0

I'm trying to use chrome.storage.local in my extension. This is a test that I coded up:

chrome.storage.local.set({'test': 'foo'}, function() {
  chrome.storage.local.get('test', function(data) {
    console.log("Test Result: ")
    console.log(data);
  })
});

var keyword = 'test2';
chrome.storage.local.set({keyword: 'foo'}, function() {
  chrome.storage.local.get(keyword, function(data) {
    console.log("Test 2 Result: ")
    console.log(data);
  })
});

The only difference between the first and the second test is that I replaced the string test by a variable holding the string test2. Now, you'd expect these both to work, right? The output is:

Test Result: 
Object {test: "foo"}
Test 2 Result: 
Object {}

What am I doing wrong?

tinloaf
  • 11
  • 3

1 Answers1

0

You cant set the key of a json object to a variable, make an object first then set the key and value

var obj = {};
obj[keyword] = 'foo'
chrome.storage.local.set(obj, function() {
Carter Cole
  • 918
  • 9
  • 16