代码之家  ›  专栏  ›  技术社区  ›  DarkLite1

无法读取axios包装器的未定义属性“then”

  •  0
  • DarkLite1  · 技术社区  · 5 年前

    我有一点类似的问题 like this 但我似乎不能把它弄对。我知道我必须这么做 return

    export const callGraph = (url, token) => {
      return axios.get(url, {headers: { Authorization: `Bearer ${token}` }})
    }
    

    这是调用的函数 callGraph Promise :

    export const getGraphProfile = () => {
      if (auth.getAccount()) {
        auth.getToken(loginRequest)
          .then(response => {
            return callGraph(graphConfig.graphMeUrl, response.accessToken)
          })
          .catch(error => { console.log(error) })
      }
    }
    

    如你所见,我明确要求 return callGraph 所以我可以这样使用它:

    getGraphProfile()
       .then(response => { console.log('givenName ', response.data.givenName) })
       .catch(error => console.log(error))
    

    出于这样或那样的原因,我仍然缺少一些东西。谢谢你的帮助。

    1 回复  |  直到 5 年前
        1
  •  1
  •   Ilijanovic    5 年前

    你应该归还axios的承诺

    export const getGraphProfile = () => {
      if (auth.getAccount()) {
        return auth.getToken(loginRequest)
          .then(response => {
            return callGraph(graphConfig.graphMeUrl, response.accessToken)
          })
          .catch(error => { console.log(error) })
      }
    }
    
    推荐文章