0

I' using async function like this:

methods:{
    async getUser(id)
    { 
 
      const header = '........';
        let responses=await axios.get(`/api/backend/method/${id}` , {headers: header});
      return responses.data.name; 

    },

calling it on load of td in a loop, like this

<td v-html="getUser(request.id)"></td>

now in my td i get [object Promise] but in this variable responses.data.name there is name, i want to return that name. Any help is highly appreciated.

2
  • 2
    You're not calling getUser with await, so the returned value is its Promise. Commented Jan 16, 2021 at 7:04
  • 1
    I would instead suggest you adjust the method to affect data, then invoke the method inside some lifecycle hook, finally the template simply reads from data - or just use vuex (with module init) Commented Jan 16, 2021 at 7:06

1 Answer 1

1
data() {
    return {
    // .... other vars
    usernames: {},
    requests: [],
    };
},
methods:{
    afterRequestLoaded() {
        this.requests.forEach((request) => this.loadusername(request.id);
    },
    async loadusername(id)
    { 
 
      const header = '........';
        let responses=await axios.get(`/api/backend/method/${id}` , {headers: header});
        this.usernames[id] = responses.data.name; 
    },
<tr v-for="request in requests">
   <td v-text="usernames[request.id]"></td>
</tr>

Note that afterRequestLoaded function should be executed when requests array is populated. perhaps by another api call.

Sign up to request clarification or add additional context in comments.

Comments

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.