0

I have a list I want to update with data from an API call - done this many times and works OK, but I must be doing something different with this solution is the list is not updating when data is returned from the API call:

<template>
Set list: {{setList}}

<div v-for="(set, index) in setList">
  {{ set.title }}
   // index is used in the loop...
</div>

</template>

<script>
import queries from '../api/queries'
export default {

 data: () => ({
   setList: []
 }),

 created() {
   this.setList = queries.getSetList();
 },
   ....

</script>



queries.js


async getSetList () {

 let setList=[];

   await API.graphql({
       setList=get the data
   }).then((response) => {
       return(setList)   
   })
 }

The print statement: Set List: [object Promise] and the list is not printed in the for loop.

The promise does return with data (showing in the dev console).

I had a similar query resolved by @IVOGELOV (thank you!) here and looking for a non vuex solution.

Thanks

1 Answer 1

1

This is not a VueJS- or a VueX-related issue per se, but more about understanding how promises and await/async work.

First you need to ensure that queries.getSetList is actually returning a promise:

async getSetList () {
    let setList = [];

    await API.graphql({
        // setList = ...
    });

    return setList;
}

And then ensure that you are actually awaiting it when invoking it:

async created () {
    this.setList = await queries.getSetList();
},
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.