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

153
Vistas
Issue when trying to interact with an API in Vuejs?

datalist.js

import axios from "axios";

export const datalist = () => {
  return axios.get("myapiurl/name...").then((response) => response);
};

HelloWorld.vue

<template>
  <div>
    <div v-for="item in items" :key="item.DttID">
      <router-link
        :to="{
          name: 'UserWithID',
          params: { id: item.DepaD },
          query: { DepaD: item.DepaID },
        }"
      >
        <div class="bt-color">{{ item.DepaName }}</div>
      </router-link>
    </div>
    <br /><br /><br />
    <User />
  </div>
</template>

<script>
import User from "./User.vue";
import { datalist } from "./datalist";
export default {
  name: "HelloWorld",
  components: {
    User,
  },
  data() {
    return {
      items: datalist,
    };
  },
  mounted() {
    datalist().then((r) => {
      this.items = r.data;
    });
  },
};
</script>

User.vue

<template>
  <div>
    <div v-for="(item, key) in user" :key="key">
      {{ item.Accv }}
    </div>
  </div>
</template>

<script>
import { datalist } from "./datalist";
export default {
  name: "User",
  data() {
    return {
      lists: datalist,
    };
  },
  computed: {
    user: function () {
      return this.lists.filter((item) => {
        if (item.DepaD === this.$route.params.id) {
          return item;
        }
      });
    },
  },
};
</script>

Error with the code is,

[Vue warn]: Error in render: "TypeError: this.lists.filter is not a function" TypeError: this.lists.filter is not a function

The above error i am getting in User.vue component in the line number '20'

From the api which is in, datalist.js file, i think i am not fetching data correctly. or in the list filter there is problem in User.vue?

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

0

Try to change the following

HelloWorld.vue

data() {
  return {
    items: [],
  };
},
mounted() {
  datalist().then((r) => {
    this.items = r.data;
  });
},

User.vue

data() {
  return {
    lists: []
  };
},
mounted() {
  datalist().then((r) => {
    this.lists = r.data;
  });
},

At least this suppress the error, but i cant tell more based on your snippet since there are network issues :)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since your datalist function returns a Promise, you need to wait for it to complete. To do this, simply modify your component code as follows:

import { datalist } from "./datalist";
export default {
  name: "User",
  data() {
    return {
      // empty array on initialization
      lists: [],
    };
  },
  computed: {
    user: function() {
      return this.lists.filter((item) => {
        if (item.DeploymentID === this.$route.params.id) {
          return item;
        }
      });
    },
  },
  // asynchronous function - because internally we are waiting for datalist() to complete
  async-mounted() {
    this.users = await datalist() // or datalist().then(res => this.users = res) - then async is not needed
  }
};

now there will be no errors when initializing the component, since initially lists is an empty array but after executing the request it will turn into what you need.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You may define any functions and import them, but they wont affect until you call them, in this case we have datalist function imported in both HelloWorld and User component, but it did not been called in User component. so your code:

  data() {
    return {
      lists: datalist,
    };
  },

cause lists to be equal to datalist that is a function, no an array! where .filter() should be used after an array, not a function! that is the reason of error. thus you should call function datalist and put it's response in lists instead of putting datalist itself in lists

Extra:

  • it is better to call axios inside the component, in mounted, created or ...
  • it is not good idea to call an axios command twice, can call it in HelloWorl component and pass it to User component via props
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