从我的函数返回结果

时间:2018-07-03 17:29:55

标签: javascript vuejs2

我如何返回结果,

我收到错误

  

找不到变量:convertToArray

我的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);
    })
    }

我在vuex商店中拥有

  // Get list of cases
  loadCasess ({ commit, context }) {
    return new Promise ((resolve, reject) => {
      apiGet()
      resolve(commit('LIST_CASES', convertToArray))
      })
      .catch(e => {
        console.error(e)
       // reject('/')
      })
    },

1 个答案:

答案 0 :(得分:1)

由于convertToArray在您的Vuex商店的上下文中不存在,因此您收到此错误。您将返回它,但这并不意味着在调用apiGet()的函数上它将存在。您应该这样写:

// 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('/')
   })
 }, // ...