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

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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