0

I am trying to save some data which should be available even when restart the browser So this data should persist. I am using Chrome Storage Sync API for this. But when I am restarting my browser, I get empty object on using chrome.storage.get.

Here is my sample code:

SW.methods.saveTaskListStore = function() {
  chrome.storage.sync.set({
    'taskListStore': SW.stores.taskListStore
  }, function() {
    if (SW.callbacks.watchProcessSuccessCallback) {
      SW.callbacks.watchProcessSuccessCallback(SW.messages.INFO_DATA_SAVED);
      SW.callbacks.watchProcessSuccessCallback = null;
    }
  });
};

SW.methods.loadTaskListStore = function() {
  SW.stores.loadTaskListStore = [];

  chrome.storage.sync.get('taskListStore', function(taskFeed) {
    var tasks = taskFeed.tasks; 

    if (tasks && !tasks.length) {
      SW.stores.loadTaskListStore = tasks;
    }
  });
};

I guess I am using the Wrong API.

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168

1 Answers1

1

If this is not some copy-paste error, you are storing under key taskListStore and trying to get data under key loadTaskListStore.

Besides that, according to the documentation on StorageArea.get(), the result object is an object with items in their key-value mappings. Thus, in your case, you should do:

chrome.storage.sync.get("taskListStore", function(items) {
    if (items.taskListStore) {
        var tasks = items.taskListStore.tasks;
        ...
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • yep loadTaskListStore was a typo..Edited the question..I think you are right I am using the API wrong way. Let me try once again with your suggestion – Sachin Jain Oct 24 '13 at 20:09