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

295
Visualizações
Vue.js cannot access elements within object

I hope someone can explain this to me..

I am using axios to fetch json from a webpage. I can get the data and output it to my html without issue, but when i try to access a single element I get an error "Cannot read properties of null (reading '0')"

Here is my JS code:

//app.js
const app = {
  data() {
    return {
      quotes: null,
    }
  },
  mounted() {
    axios
      .get('https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/')
      .then(response => (this.quotes = response.data.quotes) )
  },
  methods: {
    test() {
      console.log(Object.keys(this.quotes))
      console.log(this.quotes)
    }
  }


}

Vue.createApp(app).mount('#app')

I can access my data fine like this:

<!--index.html-->
...
<div id="app">
    <div>
      {{ quotes }}
    </div>
  </div>
...

which outputs:

[ { "quote": "Life isn’t about getting and having, it’s about giving and being.", "author": "Kevin Kruse" }, ...]

But when I try:

<!--index.html-->
...
<div id="app">
  <div>
    {{ quotes[0] }}
  </div>
</div>
...

I get the error message:

Uncaught TypeError: Cannot read properties of null (reading '0')

Can anyone help me understand how I can access a single element within my quotes object?

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

0

You trying to access an object before initialization. Try:

<div id="app">
  <div v-if="quotes">
    {{ quotes[0] }}
  </div>
</div>

At the time when the first render happened your quotes === null. And it is not possible to do null[0]. After API call you assign an object to quotes and only after rerender you able to get quotes[0]

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