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

123
Visualizações
Undefined data variable Vue.js when using axios

this is driving me nuts.

I'm assigning the value from an axios response to my vue data like this:

mounted() {
  axios
  .get('/campaigns/new.json')
    .then(response => (
      this.kits = response.data[0].kits,
     )
  )

I can see with vue developer tools that my this.kits has an array of 8 items (correct)

enter image description here

When I try to use this.kits afterwards or do console.log(this.kits), I get undefined or empty array.

What the hell am I missing? Please help. Thank you

mounted() {
  axios
  .get('/campaigns/new.json')
    .then(response => (
      this.kits = response.data[0].kits,
      this.kitProducts = response.data[0].kitproducts,
      this.products = response.data[0].products,
      this.boxes = response.data[0].boxes,
      this.categories = response.data[0].categories,
      this.extras = response.data[0].extras,
      this.isCurrentUser = response.data[0].user,
      this.giftpacks = response.data[0].giftpacks
     )
  )
  console.log(this.kits)

console.log(this.kits) will output:

enter image description here

almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

your console.log is being executed right after the promise request is initiated, you would have to place it at the end inside the then block or, as Nicola says, use async await

try this:

async mounted() {
  const response = await axios.get('/campaigns/new.json')
  this.kits = response.data[0].kits
  this.kitProducts = response.data[0].kitproducts
  this.products = response.data[0].products
  this.boxes = response.data[0].boxes
  this.categories = response.data[0].categories
  this.extras = response.data[0].extras
  this.isCurrentUser = response.data[0].user
  this.giftpacks = response.data[0].giftpacks

  console.log(this.kits)
}
almost 4 years ago · Santiago Trujillo Relatório

0

Try to wait for your data. Move the API call to method:

methods: {
  async getData() {
    const res = await axios.get('/campaigns/new.json')
    this.kits = res.data[0].kits,
  }
}

and then in mounted hook:

async mounted() {
  await this.getData()
  console.log(this.kits)
}
almost 4 years ago · Santiago Trujillo 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