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

161
Visualizações
How get uncached value from localStorage

I store Access token for authorization in javascript localStorage. Every time when my frontend app loaded I want get this token for get user.

  created() {
    let access = localStorage.getItem('access')
    console.log(access)
    if (!access) {
      console.log('access not exists')
    } else {
      console.log('access exists:', access)
    }
  }

But this token should has overwrited quite often. And when I clean this token because user logout I should get null value in Access. But chrome cache this value and 'if' block still get token like existed. This what i see in console:

How I can get uncached value? Or I should leave this. And it will disturb in production?

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

0

Local Storage is not cached. I assume the issue is the way how you clean local storage. I assume you are doing something like this: localStorage.setItem('access', undefined). This will set the local storage item to the string 'undefined', and if you do if (!access) it will always return false. You currently cannot use undefined, null or true / false in local storage, only strings. You can check the stored type by printing the returned type: console.log(typeof localStorage.getItem('access')), which should be string.

You have 2 options:

Remove the item completely from local storage

localStorage.removeItem('access')

Then you can use your current implementation, as local storage will return undefined on localStorage.getItem('access').

Set an empty string and test for string length:

// "clean" your access
localStorage.setItem('access', '');

// Check if there is an access
const access = localStorage.getItem('access');
if (access && access.length > 0) {
  console.log('access does exists')
} else {
  console.log('access does NOT exists')    
}
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