19

I have 4 items which I'd like to get but I'm unsure how to separate the keys. Using comma gives an error. Here is an example of my usage:

chrome.storage.sync.get({
    'customImage',
    'customColor',
    'customRandColor',
    'customRandImage'
  }, function(backgroundCheckedOptions) {
    document.getElementById('optionsCustomImage').checked = backgroundCheckedOptions.customImage;
    document.getElementById('optionsBackgroundColor').checked = backgroundCheckedOptions.customColor;
    document.getElementById('optionsRandomColor').checked = backgroundCheckedOptions.customRandColor;
    document.getElementById('optionsRandomImage').checked = backgroundCheckedOptions.customRandImage;
  });

I would have assumed they would be separated by a comma, but I guess not.

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
Jamie
  • 421
  • 4
  • 17

2 Answers2

23

From the Chrome Storage documentation, it says:

StorageArea.get(string or array of string or object keys, function callback)

Easiest would be to pass an array by replacing your {} with []

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • 7
    Object form is also useful for giving default value: `{key1: default1, key2: default2}` – Xan Mar 24 '15 at 13:50
1

As per the official documentation it will be as below.

chrome.storage.sync.get([
 'customImage',
 'customColor',
 'customRandColor',
 'customRandImage'
], function(backgroundCheckedOptions) {
 document.getElementById('optionsCustomImage').checked = backgroundCheckedOptions.customImage;
 document.getElementById('optionsBackgroundColor').checked = backgroundCheckedOptions.customColor;
 document.getElementById('optionsRandomColor').checked = backgroundCheckedOptions.customRandColor;
 document.getElementById('optionsRandomImage').checked = backgroundCheckedOptions.customRandImage;
});
Captain Sparrow
  • 1,114
  • 17
  • 26