Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
Unable to get array from chrome.storage

Whenever I try to get data from chrome.storage, it returns undefined.

chrome.storage.sync.get('courses', function(result) {
      currentCourses = result.courses
      console.log(currentCourses)
      currentCourses.push(coursename)
      chrome.storage.sync.set({courses: currentCourses}, function() {
        alert(`Course "${coursename}" has been added !`)
      });
}
userCourses = async () => {
  await chrome.storage.sync.get('courses', function(result) {
      return(result.courses)
  });
}
courseData = userCourses()
console.log(courseData)

I found an answer here on stack overflow, but it is giving the same error. I am new to JS, and not comfortable with Promises.

I tried the example from the official docs, but it is also giving the same error as below. Can someone help me with a solution, or modify my code ? Error

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You use chrome.storage.sync.get() with a completion callback. But notice that the data you retrieve using that .get() method isn't available except within the callback.

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

is the example from the docs. It is the invocation of a function. But that function always and necessarily returns undefined.

Notice that the first parameter to .get() is an array of keys. Your code lacks the array.

The second parameter is the callback function. It looks like this:

function (result) {
   
}

Within the function result.key is the key you requested, and result.value is whatever you stored in the key. You're looking at result.courses, which isn't defined.

Control flow.

  1. The call to .get() completes immediately, always.
  2. At some later time the callback is called.
about 4 years ago · Juan Pablo Isaza Relatório

0

Wrap the call to storage in a promise and await it:

function getStorageValuePromise(key) {
  return new Promise((resolve) => {
    chrome.storage.sync.get(key, resolve);
  });
}

await getStorageValuePromise('val');
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda