【问题标题】:How can I console log axios response outside the request如何在请求之外控制台记录 axios 响应
【发布时间】:2021-06-27 08:28:38
【问题描述】:
async login(context, payload) {
      const response = await axios
        .post(
          'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyCQ6w2jvJVNrOwON4-KnEOV1kH-ckEDokg',
          {
            email: payload.email,
            password: payload.password,
            returnSecuredToken: true
          },
          {
            Headers: {
              'Content-Type': 'application/json'
            }
          }
        )
        .then(function(response) {
          console.log(response);
        })
        .catch(function(error) {
          console.log(error);
          console.log(error.response);
        });

      console.log(response);
      
      context.commit('setUser', {
        token: response.data.idToken,
        userId: response.data.userId,
        tokenExpiration: response.data.expiresIn
      });

你好,也许这是一个愚蠢的问题,但我怎样才能在那里控制台记录我的回复?我试图将我的响应存储在 const 中,但这是同样的问题,控制台日志和提交在等待异步之前执行,我无法使用从 axios 响应返回的任何数据,谢谢您花时间帮助我.

【问题讨论】:

    标签: vue.js axios


    【解决方案1】:

    问题是你的中间 .then 什么都不返回,所以 await axios.post() 的返回值解析为 undefined

    您可以删除不必要的.then

    const response = await axios.post(/*...*/);
    console.log(response);
    

    ...或在.then 中返回response

    const response = await axios.post(/*...*/)
                       .then(response => {
                         console.log(response);
                         return response;
                       });
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您不需要 then 函数。有await的话就够了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-17
        • 1970-01-01
        • 2017-12-23
        • 1970-01-01
        • 2019-01-10
        • 2019-06-11
        • 1970-01-01
        相关资源
        最近更新 更多