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

231
Views
Axios request goes into infinite loop on VueJs

I have created an axios function that fetches images and displays them on a webpage. The problem is, when the axios makes the request, it goes into an infinite loop of requests and never stops. The number in the blue bubble just keeps increasing rapidly.

enter image description here

I am not sure what the problem. Any help will be appreciated.

this a Slide component that renders photos in a slider

          <Slide
            v-for="(post, index) in getAllInstaPosts"
            :key="index"
            :class="`slide slide-${index}`"
            :aria-hidden="index === 0 ? false : true"
          >

it get it's information from this method in computed property - I tried using async keyword in here, but the compiler kept complaining - unexpected async

getAllInstaPosts () {
      return (this.item.pdp && this.item.pdp === true) ? this.getInstaPostsForProduct() : this.allInstaPosts
    }

this function in methods property that creates the request.

 getInstaPostsForProduct () { // <- makes a call to foursixty api to fetch insta posts of a certain product
          axios.get(`https://foursixty.com/api/v2/${foursixty.sitename}/timeline/?for_url=https://www.${foursixty.domainName}/${this.$router.currentRoute.path}`)
            .then(response => {
              if (response.status === 200) {
                this.postsForUrl = response.data.results
                console.log('DATA:' + response.data)
              }
            })
            .catch((err) => {
              Logger.error(err);
            })
          return this.postsForUrl;
        }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Computed property functions must be synchronous. You cannot return the response from an async function
  2. Computed properties should be as close to pure functions as possible. Creating side-effects like altering data properties leads to infinite loops since computed properties are recomputed every time any of their dependencies change.

The solution is to store state in your data properties and update it at the appropriate time such as when your current route changes

export default {
  data: () => ({
    allInstaPosts: [], // ¯\_(ツ)_/¯
    postsForUrl: [],
    item: {
      pdp: true // ¯\_(ツ)_/¯
    }
  }),
  computed: {
    getAllInstaPosts () {
      return this.item.pdp ? this.postsForUrl : this.allInstaPosts;
    }
  },
  methods: {
    async getInstaPostsForProduct () {
      this.postsForUrl = []; // empty the array
      try {
        const { data: { results } } = await axios.get(
          `https://foursixty.com/api/v2/${foursixty.sitename}/timeline/`,
          {
            params: {
              for_url: `https://www.${foursixty.domainName}/${this.$route.path}`
            }
          }
        );
        this.postsForUrl = results;
      } catch (err) {
        Logger.error(err);
      }
    }
  },
  watch: {
    $route: { // watch for route changes and trigger the request
      immediate: true,
      handler: "getInstaPostsForProduct"
    }
  }
};
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!