0

I am saving a key value pair using chrome.sync.set in background.js and retrieving it from options.js.

Background.js:

var tempMap = {};
function function1() {


     chrome.tabs.query({},function(tabs) {

      //  tempMap["kk"] = "value";  //LINE 1
      });

     // tempMap["kk"] = "value";  //LINE 2

     save['Map_stored'] = tempMap;

     chrome.storage.sync.set(save, function() {
          console.log(save); //LINE 3
     });
}

If LINE 1 is uncommented:

  • options.js shows empty object for stored value

If LINE 2 is uncommented:

  • options.js shows {"kk":"value"}

What is more weird is: Line 3 always shows the right value.

I spent flat 3 hours just to trace this "bug". I hope this helps someone in future.

dasfdsa
  • 7,102
  • 8
  • 51
  • 93
  • You should be declaring your variables `var` and `let` are your friends. – Makyen Dec 09 '16 at 18:49
  • This is not my actual code. This is just a part of it to make the point. Also, I am not using Map as variable. Let me edit it – dasfdsa Dec 09 '16 at 18:52
  • In the future, keep in mind that the *only* information that we have is what is in the question. That is why a **complete** [mcve], that duplicates the problem, is required for debugging questions. – Makyen Dec 09 '16 at 18:54
  • Just FYI: What you are seeing is what I would expect to happen given your code. Please read/study the "possible duplicate" mentioned in an earlier comment. If, after going through that, you still have questions, then either edit your question to explain what portion of this still not understanding, or ask a new question with more detail as to what you are not understanding. – Makyen Dec 09 '16 at 19:25

0 Answers0