[Vuejs]-Why am i getting [Object Promise], how to work with a promise

0๐Ÿ‘

โœ…

I believe you are overcomplicating the response fetching process. See if the following adjustments help, and please be sure to only call the next APIs subsequently (right after this token is set), that is when the first Promise is resolved.

export default {
  data () {
    return {
      token: ''
    }
  },

  async mounted () {
    await this.displayAccessToken();

    console.log('token:' + this.token);
  },

  methods: {
    async displayAccessToken() {
      this.token = await axios({
        method: 'POST',
        'url': 'my_api_end_point',
        'auth': {
          'username': 'my_username',
          'password': 'my_password'
        },
        'headers': {
          'content-type': 'application/json'
        }
      })
      .then(function(response) {
        console.log('response:' + response);

        return response.data.access_token;
      });
    }
  }
}

Also, if all this method does is returning a token, I would probably make it a void function and call the method setAccessToken instead, but of course that would depend on your use case.

Leave a comment