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

178
Visualizações
cant access json object stored in local storage [object Object] in vue.js

I have fieldObj which I need frequently. It comes from database. I want to store it in localStorage.

below is the code I wrote which fetch fieldObj from database if localStorage don't have it.

When I try to access the json data stored in localStorage it just shows

[object Object]

code is treating fieldObj of localStorage as string.

I need to access key values pairs stored in fieldObj.

<script>
export default {
  data() {
    return {
      fields: JSON,
    }
  },
  created() {
    if (!localStorage.getItem('field-obj')) {
      axios
        .get('/api/u/record/', {
          withCredentials: true,
        })
        .then((response) => {
          response.data.forEach((el) => {
            if (el.COLUMN_TYPE == 'int') el.COLUMN_TYPE = 'number'
          })
          const fieldObj = response.data[0].fieldObj[0]
          this.fields = fieldObj
        })
        .then(() => {
          localStorage.setItem('field-obj', this.fields)
        })
    } else {
      console.log(new Object(localStorage.getItem('field-obj')))
      // console.log(localStorage.getItem('field-obj')[0].username)
    }
  },
}
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Setting it in the localStorage like this

localStorage.setItem('field-obj',JSON.stringify(this.fields))

and accessing it with this fixed the issue

this.fields = JSON.parse(localStorage.getItem('field-obj'))

A more complex example could be found here, using Nuxt.

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