Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
Consuming API not working, what am I doing wrong here?

Not sure if this is the place to change things, but I have changed some things due to your comments, I stil cant see the pictures, but here is an update of the code:

JS app:

    import axios from 'axios';
export default {
    name: 'home',
    components: {
        ImageCard
    },
    data() {
        return {
            images: []
        }
    },
    methods: {
        fetchImages = () => {
            axios
                .get("https://foodish-api.herokuapp.com/api/images/pizza")
                .then(res => {
                    console.log(res);
                    this.setState({ images: res.data.message });
                })
                .catch(err => console.log(err));
            this.images = fetchImages()
            });
        }

Index.cshtml code here:

<div id="app">
<div class="home">
    <li v-for="image in images" :key="image.id">
        <img :src="image.url" :alt="image.alt" />
    </li>
    
</div>

Obviously something wrong here but i cant seem to get it right since I cant see the pictures, what is still wrong here? Also, when your refer to something I should do different, please just point me in the right direction of the code :D

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you did wrong the following:

  1. create a method fetchImages and never call it.
  2. this.setState this undifined function.
  3. this.images = fetchImages() will lead you to infinite loop because this is a recursion function
  4. in your html use :src="image.url" to show the image but you get the data as string not object and you must use :src="image"

you can check the solution in this codesandbox project

https://codesandbox.io/s/busy-ellis-rcifm?file=/src/App.vue

<template>
  <div id="app">
    <li v-for="image in images" :key="image.id">
      <img :src="image" :alt="image.alt" />
    </li>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "App",
  data() {
    return {
      images: [],
    };
  },
  mounted() {
    axios
      .get("https://foodish-api.herokuapp.com/api/images/pizza")
      .then((res) => {
        this.images = res.data;
      })
      .catch((err) => console.log(err));
  },
};
</script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Firstly,when you call fetchImages(),you need to set this.images=fetchImages(),so that the data of images will be set.And if you want to display pictures,you can do like this:

<li v-for="image in images" :key="image.id">
     <img :src="image.url" :alt="image.alt"/>
</li>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda