Search code examples
javascriptgoogle-chrome-extension

Cannot set value in chrome.storage


I have no idea why I can't set a value in chrome.storage.sync (same issue with storage.local), I've tried multiple solutions online, but sadly none of them work

This is my popup.js file (linked to the popup.html file)

window.addEventListener("DOMContentLoaded", () => {
    let my_key = "some string";
    let value = "some value"

    chrome.storage.sync.set({[my_key]: value}, function() {
        console.log('Value is set to ' + value);
    });

    chrome.storage.sync.get([my_key], function(result) {
        console.log('Value currently is ' + result.key);
    });
});

Everytime the output is Value currently is undefined.


Solution

  • You should use result[my_key] not result.key

    chrome.storage.sync.get([my_key], function(result) {
            console.log('Value currently is ' + result[my_key]);
        });