3

So I'm in a bit of a dillema right now. I can't seem to be able to add an Array in chrome.storage and later retrieving it. Here's the code I have right now:

function() {
    chrome.storage.sync.get({ObjectName: []}, function (result) {
    var ObjectName = result.ObjectName;
    ObjectName.push({ArrayName: document.getElementById("field")});
    });

Now to retrieve it and display it:

chrome.storage.sync.get({ArrayName: function(value) {
            for(i=0; i<value.length; i++) { 
                document.write(value)
            };

The error I am getting, which might be as simple as a syntax issue, amounts to:

Error: Invocation of form get(object) doesn't match definition get(optional string or array or object keys, function callback)

Hamza Ibad
  • 41
  • 1
  • 2
  • 2
    store and load JSON in strings, you can't directly use objects – dandavis Jun 29 '15 at 18:20
  • 2
    Actually, you can store object/arrays directly with the Storage API. See: http://stackoverflow.com/questions/16605706/store-an-array-with-chrome-storage-local – Cymen Jun 29 '15 at 18:26
  • I don't see any differences between the linked solution to mine yet I'm still getting the same error. – Hamza Ibad Jun 29 '15 at 18:59
  • Your second get isn't correct syntax. You have `get(object)` where `object` is `{ArrayName:function()}`. You need `get(object,callback)`. – Teepeemm Jun 30 '15 at 13:54

1 Answers1

5

You have to use set method to set values to chrome.storage

Here's an example of how to do it

To store an array to chrome storage using set

var testArray=["test", "teste", "testes"];

chrome.storage.sync.set({
    list:testArray
}, function() {
    console.log("added to list");
});

To get the arrayValue using get and modify if by calling updatemethod

chrome.storage.sync.get({
    list:[]; //put defaultvalues if any
},
function(data) {
   console.log(data.list);
   update(data.list); //storing the storage value in a variable and passing to update function
}
);  

function update(array)
   {
    array.push("testAdd");
    //then call the set to update with modified value
    chrome.storage.sync.set({
        list:array
    }, function() {
        console.log("added to list with new values");
    });
    }
Damzaky
  • 6,073
  • 2
  • 11
  • 16
Madhan
  • 5,750
  • 4
  • 28
  • 61