Search code examples
javascriptarraysjsonfirefox-addonfirefox-addon-sdk

Firefox SDK simple storage loses associative array data after restart


About

I'm working on an add-on with the Firefox SDK. In the addon I store an associative array with simple-storage.


Code

Associative array I store with simple prefs

var spammer = [];
spammer['id'] = res[0];
spammer['username'] = res[1];
spammer['date'] = res[2];

ss.storage.spammers.push(spammer);

console.log(ss.storage.spammers[i]['id']);
console.log(ss.storage.spammers[i]['username']);
console.log(ss.storage.spammers[i]['date']);

This seems to work, while the add-on is active the right values are logged.

screenshot1

However when I the browser the following code is run

require("sdk/tabs").on("ready", function(tab) {
    for (i = 0; i < ss.storage.spammers.length; i++) {
        console.log(ss.storage.spammers[i]['id']);
        console.log(ss.storage.spammers[i]['username']);
        console.log(ss.storage.spammers[i]['date']);
    }
});

But now when I run the code is run 'undefined' values are logged

screenshot2

(the first 3 lines are from previous session stored with simple-prefs, the 3 working lines are from the new session)

  • the correct number of logs are made, so the correct number of values are present
  • I use the SDK profiles correctly


Question

Is it not possible to store associative arrays with simple-storage or is something else the matter?


Solution

  • In

    var spammer = [];
    spammer['id'] = res[0];
    spammer['username'] = res[1];
    spammer['date'] = res[2];
    
    ss.storage.spammers.push(spammer);
    
    console.log(ss.storage.spammers[i]['id']);
    console.log(ss.storage.spammers[i]['username']);
    console.log(ss.storage.spammers[i]['date']);
    

    Change var spammer = []; to var spammer = {};