1

I am trying to store and get objects by using chrome.storage for my chrome extension and want to do it simultaneously with two different key-value pairs. Doing this individually works but I need to set and get both pairs simultaneously and that is not working. I haven't seen much documentation on multiple key-value pairs with chrome.storage which is the reason for my confusion. This is the code that I am trying to use, how do I fix it?

  var key = 'key', stored1 = {'val': object1.innerHTML};
  var key2 = 'key2', stored2 = {'val': object2.innerHTML};
  chrome.storage.local.set({[key]: stored1, [key2]: stored2}, function() {
     console.log('Saved', key, stored1);
     console.log('Saved', key2, stored2);
  });

function fun() {
      chrome.storage.local.get(['key', 'key2'], function(result) {
        if (Object.values(result)[0] != undefined) {
              something = Object.values(result)[0].val
        }
        if (Object.values(result)[1] != undefined) {
              gpaTable.innerHTML = Object.values(result)[1].val;
        }
       });
    }
nosh
  • 620
  • 3
  • 14
  • 50
  • Does this answer your question? [How to use a variable for a key in a JavaScript object literal?](https://stackoverflow.com/questions/2274242/how-to-use-a-variable-for-a-key-in-a-javascript-object-literal) – wOxxOm Dec 21 '19 at 19:20
  • You say that storing those values individually does work. Can you show the code? – Anton Dec 21 '19 at 19:20
  • See the linked topic: in JavaScript a variable key should be used as `{[keyVar]: value}` when creating an object and as `object[keyVar]` when reading it. – wOxxOm Dec 21 '19 at 19:26
  • When console.loging the results it works with your code, maybe getting or saving the results is broken? – Marcin Dec 21 '19 at 19:28
  • I updated the code a little bit to set({[key]:value, [key2]: value}), but getting the value of the second key still isn't working. – nosh Dec 21 '19 at 19:36
  • When do you invoke `fun()`? Sounds like a timing issue: since the API is asynchronous you should read the data only after it's written which happens in the callback of set(). Also, simply use `result[key]`. – wOxxOm Dec 21 '19 at 19:42
  • I should have specified. I invoke set before the webpage unloads. The get is called after the page first loads. That is why I have if statements to see if the object exists or not. Also I'm not very familiar with controlling asynchronous functions, how can I wait until chrome.storage.local.get() and chrome.storage.local.set() are finished? – nosh Dec 21 '19 at 19:50

1 Answers1

0
function fun() {
      chrome.storage.local.get(['key', 'key2'], function(result) {
        result.key+result.key2; //you can use it like this
    }
defend orca
  • 617
  • 7
  • 17
  • 1
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm May 08 '20 at 08:47
  • you can edit it, and i think the code Self-explanatory – defend orca May 08 '20 at 12:17