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

144
Visualizações
Vue/Nuxt lifecycle calling a promise in beforecreate and the getter in created

I am attempting to do a set of a variable in my beforeCreate() for a component and then in the created() get that variable. Essentially I need to set the variable in my store then get that variable. My created calls an action that runs an axios promise that commits the data to the state, and I have a getter in the store that I want to call in my created() to get that data.

I know I am getting the correct data back from my promise as I can see it in my console log, I just cant seem to get that data from my getter.

It doesn't seem to want to work, and I think it has to do with the fact that axios has not returned the promise yet? How can I ensure that my created() waits until the axios promise has been returned in beforeCreate()?

Sample code

..... Component....

data(){
  return{currentUser: null}
},
beforeCreate(){
  this.$store.dispatch('users/setCurrent')
}, 
created(){
 this.currentUser = this.$store.getters['users/current'].displayname
}

......Store.....

export const state = () => ({
  current: {}
});

export const mutations ={
  setCurrent: (state, c) => (state.current = c)
}

export const getter = {
  current: state => state.current
}

export const actions = {
setCurrent({commit, dispatch, rootState}){
   this.$axios.$get('/api/user/current', {headers: rootState.sessionUser.current})
   .then(res => {
     console.log(res)
     commit("setCurrent" res)
  })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try to wait for response in your actions:

export const actions = {
  async setCurrent({commit, dispatch, rootState}){
    await this.$axios.$get('/api/user/current', {headers: rootState.sessionUser.current})
      .then(res => {
      console.log(res)
      commit("setCurrent" res)
  })
}

and then in created or mounted hook :

async created() {
  await this.$store.dispatch('users/setCurrent')
  this.currentUser = this.$store.getters['users/current'].displayname
}
  
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