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

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

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 Report

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 Report

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