0

How do I return my result please,

I get an error

Can't find variable: convertToArray

I have a function in my api.js

export function apiGet() {
    return axios
    .get('http')
    .then(res => {
        const convertToArray = []
        for (const key in res.data) {
            convertToArray.push({ ...res.data[key], id: key })
            //console.log confirms my convertToArray has the info I expect in it
        }

        return convertToArray;
        })
        .catch(e => {
        console.log(e);
    })
    }

in my vuex store I have

  // Get list of cases
  loadCasess ({ commit, context }) {
    return new Promise ((resolve, reject) => {
      apiGet()
      resolve(commit('LIST_CASES', convertToArray))
      })
      .catch(e => {
        console.error(e)
       // reject('/')
      })
    },
Paul
  • 581
  • 3
  • 10
  • 19
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – zero298 Jul 03 '18 at 17:33
  • `.then()` your `apiGet()` – zero298 Jul 03 '18 at 17:33
  • 2
    Is this your actual code, or did you remove some context or make some typos in copying into your question? Are you familiar with JavaScript promises and how they work? – Chad Moore Jul 03 '18 at 17:33

1 Answers1

1

You're receiving this error because convertToArray doesn't exist in the context of your Vuex Store. You're returning it, but doesn't mean that on the function that calls the apiGet() it will exist. You should write like:

// Get list of cases
loadCases ({ commit, context }) {
   return new Promise ((resolve, reject) => {
      const convertToArray = apiGet()
      resolve(commit('LIST_CASES', convertToArray))
   }).catch(e => {
         console.error(e)
        // reject('/')
   })
 }, // ...
reisdev
  • 3,215
  • 2
  • 17
  • 38