1
votes
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
      });

Hello, maybe it's a dumb question but how can I console.log my response there ? I tried to stock my response in a const but it's the same issue, the console log and the commit execute before the await async and I can't use any data that return from the axios response, thanks if you take the time to help me.

2

2 Answers

1
votes

You don't need then function in this case. With await word is enough

0
votes

The problem is your intermediate .then returns nothing, so the return value of await axios.post() resolves to undefined.

You could either remove the unnecessary .then:

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

...or return response in .then:

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