3

I have two v-for loops like so:

<div v-for="id in ids">
        <div v-for="data in getData(id)">
              <p>{{data.name}}</p>
        </div>
</div>

The first v-for iterates through an array of ids stored inside data(). On the second v-for it iterates through the result returned by the function getData().

The axios call does return an array of result including the name per ids, however the <p> tag doesnt display the names.

<script>
    data(){
       id: ['1', '2', '3']
    },
    methods:{
       getData(id){
          //axios call
          return response.data;
       }
    }
</script>
4
  • thats not good. using methods in v-for Commented Nov 25, 2020 at 7:03
  • do you have a different approach that will give the desired result? pls do suggest Commented Nov 25, 2020 at 7:10
  • May we see the response.data object. I would also like to suggest that this is a fairly inefficient way of conducting what I believe is an operation to get information of multiple models. You're better off creating an endpoint that gets the info of multiple models at once. Commented Nov 25, 2020 at 7:20
  • Took your advice that it is a bad way of implementing this approach, I then was able to make a working method that ran after the axios call. The method stored all the data I needed. Thank you. Commented Nov 25, 2020 at 7:47

1 Answer 1

2

I think this is the problem:

when v-for calls your method it doesn't wait for axios call to resolve and since there is no array immediately available for it to iterate on, it can't show anything (there isn't anything to show), on the other hand when axios call does resolve there is no way for v-for to get notified and therefore update the template. so situation is something like this:

  1. v-for runs your method
  2. method doesn't return an array immediately and v-for can't wait
  3. there is nothing to show here and it moves on

so here is what you can do:

  1. bind the v-for to an array in data object
  2. use axios result to update the said array

and it should work fine, your code might be like this for example:

// template
<div v-for="data in axiosResult">
   <p>{{data.name}}</p>
</div>

//script
<script>
data(){
   id: ['1', '2', '3'],
   axiosResult: [],
},
methods:{
   getData(){
      //axios call
      this.id.forEach((id) => {
         //axios function for each id
         axiosCall(id).then((result) => {
          this.axiosResult.push(result);
         });
      });
   }
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I decided to create another method, which exactly does the same thing. I will still mark this as the correct answer, for others to reference from and for your effort. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.