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

128
Visualizações
Vue won't render data for specific API request

So I have next component:

<template>
  <div class="home">
    <div class="container"  v-if="data" >
      <Card v-for="result in data" :img="result.links[0]" :key="result.href"/>
    </div>
  </div>
</template>

<script>
 import Card from "../components/Card";
 import axios from "axios";
 export default {
   name: 'Home',
   components: {Card},
   data() {
     return {
       data: null,
     }
   },

   created() {
      axios.get('https://images-api.nasa.gov/search?q=mars').then(res => {
         return res.data.collection.items
      }).then(res => {
         this.data = res;
      })
    }
 }

I have problem when I render Card component because I cant pass "img" prop, console shows "Cannot read properties of undefined (reading '0')" error, which is weird because I can see that my "data" property has got right data from API, but when I try to fetch data from same API with different query param like "https://images-api.nasa.gov/search?q=jupiter" everything works correctly. So I don't know if this could be Vue internal problem or an API problem?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can modify your code a bit to make sure that result.links is not undefined

<Card v-for="result in data" :img="getImageLink(result)" :key="result.href"/>

Now in your methods,

        getImageLink(result) {
            if (result.links) {
                return result.links[0];
            }

            return null;
        }

This would solve your problem

over 4 years ago · Santiago Trujillo 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