Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
confusing error in Vue event Uncaught (in promise) TypeError: _this.$auth.getAccessToken(...).then is not a function

To prefeace this, I'm a novice at JS programming, I'm a .net guy, so this may be the source of the cofusion.

On my PageLayout.vue, in the created event, I'm trying to do authentication stuff (using okta)

The error I'm getting is:

Uncaught (in promise) TypeError: _this.$auth.getAccessToken(...).then is not a function at eval (PageLayout.vue?66f1:87)

but right above the call to getAccessToken, the call to this.$auth.GetUser() is a success, we are literally in the then call of that method.

Does the this change scope in the then call?

Then why does this.$store.commit('SET_OKTA_USER', user) succeed right before it?

created() {
    this.$store.commit('SET_LOADING_APP', true)

    this.$auth.getUser().then((user) => {
      // Set Okta user
      if (user == null && user == undefined) {

        // no user
        this.$store.commit(
          'SET_RESPONSE_MSG',
          'Okta user is null or undefined.'
        )
        this.$store.commit('SET_RESPONSE_TYPE', 'error')
      } else {
        // store user
        this.$store.commit('SET_OKTA_USER', user)

        // store token
        this.$auth.getAccessToken().then((token) => { //<---------blow up here saying it's not a function.
          this.$store.commit('SET_ACCESS_TOKEN', token)
          localStorage.setItem('token', JSON.stringify(token))
          apiClient.defaults.headers.common.Authorization = `Bearer ${token}`
        })
        // adName
        const adName = user.preferred_username.substr(
          0,
          user.preferred_username.indexOf('@')
        )
        this.$store.commit('SET_AD_NAME', adName)
        this.getUserInfo(adName)
      }
    })
  },

I'm at a loss as to why the code above it is valid, but the getAccessToken is not.

ifi paste

  const accessToken = this.$auth.getAccessToken();
  if (accessToken != undefined)
  {
      this.setUserToken(accessToken);
      this.getUserInfo(this.$store.state.adName);
  }

Immediately above the this.$auth.getAccessToken().then... it appears to work fine.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I feel pretty confident that this is a variable scope issue, but I'm not entirely sure how to describe it.

Basically the anonymous function that's being passed to then is creating a new private scope,

this.$auth.getUser().then((user)=>{
  //this no longer exists like you expect it to
});

The simplest solution that occurs to me would be to change your created() method to async so that you can rework it to something like this:

async created(){
  let user = await this.$auth.getUser();
  //do some stuff
  let token = await this.$auth.getAccessToken(); 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda