Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
chrome.storage.sync.get value after return is always undefined

I am programming a chrome extension. It works good but i have a problem getting the value.

On installation it should set a default value. But here comes the problem: I cant get the value. logValue() should give the value to the console for testing, which it takes from getValue(). In the getValue() function, it works but if I return the value, it is undefined. (Details in the screenshot below.

What is my fault and how can I do it, that the value and type in the logValue() function is the same as in the getValue()?

And I am sorry for my bad english:)

Devtools output

Here you see, in the getValue() function it has the right value (1st time null and 2nd time the string of the object. But if I return it to the logValue() function which replaces a function to work with it, the value is only undefined.

And here is my code:

function logValue() {
    var x = getValue();
    y = JSON.stringify(x);
    console.log("Type: " + typeof (x) + "\nvalue: " + x);
}

function getValue() {
    debugger;
    chrome.storage.sync.get(['testObject'], function (result) {
        var value = result.testObject;
        console.log("Type: " + typeof (value) + "\nvalue: " + value);
        return value;
    });
}

function setDefaultValue(data) {
    debugger;

    data = JSON.stringify(data);
    chrome.storage.sync.set({
        'testObject': data
    }, function () {
        console.log('Value is set to ' + data);
    });
}

chrome.runtime.onInstalled.addListener(() => {
    debugger;
    chrome.storage.sync.set({
        'testObject': null
    }, function () {});

    data = {
        "data": [{
                "name": "Test1",
                "value": "Value1"
            },
            {
                "name": "Test2",
                "value": "Value2"
            }
        ]
    };
    logValue();
    setDefaultValue(data);
    logValue();

});

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!