Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!