Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!