26

I'm writing a chrome extension, and I can't store an array. I read that I should use JSON stringify/parse to achieve this, but I have an error using it.

chrome.storage.local.get(null, function(userKeyIds){
    if(userKeyIds===null){
        userKeyIds = [];
    }
    var userKeyIdsArray = JSON.parse(userKeyIds);
    // Here I have an Uncaught SyntaxError: Unexpected token o
    userKeyIdsArray.push({keyPairId: keyPairId,HasBeenUploadedYet: false});
    chrome.storage.local.set(JSON.stringify(userKeyIdsArray),function(){
        if(chrome.runtime.lastError){
            console.log("An error occured : "+chrome.runtime.lastError);
        }
        else{
            chrome.storage.local.get(null, function(userKeyIds){
                console.log(userKeyIds)});
        }
    });
});

How could I store an array of objects like {keyPairId: keyPairId,HasBeenUploadedYet: false} ?

little-dude
  • 1,544
  • 2
  • 17
  • 33
  • 3
    There is no need to stringify/parse it. You can store arrays directly. – BeardFist May 17 '13 at 09:32
  • @BeardFist I have Uncaught TypeError: Object # has no method 'push' for the following code, that's why I thought I should stringify my array. `chrome.storage.local.get(null, function(userKeysIds){ if(userKeysIds===null){ userKeysIds = new Array(); } userKeysIds.push({keyPairId: keyPairId,HasBeenUploadedYet: false}); // ERROR chrome.storage.local.set(userKeysIds,function(){ } }); });` – little-dude May 17 '13 at 11:21
  • You get and set stuff using `keys` and when you get it, it is an object such as `chrome.storage.local.get('userKeyIds', function(stuff){console.log(stuff.userKeyIds);});` – BeardFist May 17 '13 at 16:28

1 Answers1

55

I think you've mistaken localStorage for the new Chrome Storage API.
- You needed JSON strings in case of the localStorage
- You can store objects/arrays directly with the new Storage API

// by passing an object you can define default values e.g.: []
chrome.storage.local.get({userKeyIds: []}, function (result) {
    // the input argument is ALWAYS an object containing the queried keys
    // so we select the key we need
    var userKeyIds = result.userKeyIds;
    userKeyIds.push({keyPairId: keyPairId, HasBeenUploadedYet: false});
    // set the new array value to the same key
    chrome.storage.local.set({userKeyIds: userKeyIds}, function () {
        // you can use strings instead of objects
        // if you don't  want to define default values
        chrome.storage.local.get('userKeyIds', function (result) {
            console.log(result.userKeyIds)
        });
    });
});
gblazex
  • 49,155
  • 12
  • 98
  • 91
  • 3
    That works perfectly, thanks a lot ! And nice tip for default values :) – little-dude May 18 '13 at 16:58
  • 1
    How come data is retrived with 'userKeyIds' in the end which is a string, when we are setting the key as userKeyIds object? Code seems to working fine. – Mike Jun 16 '16 at 05:29
  • The getter **also accepts an object** if you want to specify *default values*: `chrome.storage.local.get({'userKeyIds': [1,2,3]}, fn)` in which case the `fn` will be called with the value of `userKeyIds` or `[1,2,3]` if it doesn't exist yet. – gblazex Jun 16 '16 at 22:07
  • If you don't need default values, however, you can simply pass the key you want to get as a string or array of strings (see the answer's code). `chrome.storage.local.get('userKeyIds', fn)` or `chrome.storage.local.get(['userKeyIds', 'otherKey', 'yetAnotherKey'], fn)` – gblazex Jun 16 '16 at 22:10