1

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

2
  • where are you doing console.log ? Commented Sep 22, 2022 at 16:58
  • @y.kaf. In my mounted() vue function Commented Sep 22, 2022 at 16:58

2 Answers 2

2

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)
}
Sign up to request clarification or add additional context in comments.

5 Comments

Still using async I keep getting an empty array :(
check my edit, should work
had to remove the commas, haha
also, had forgotten the await, now it's correct, sorry
yeah, asynchronous can be tricky, but you'll get use to it
1

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)
}

3 Comments

No luck. Still I need to be able to use this.kits in my mounted and will show up as an empty array :(
@Gibson hey mate, did you try it? with async/await you should be able to see the data.
Nikola, tried it with no luck, André answer worked using await, thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.