Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda