[Vuejs]-Can't change my array, because I've got Scope Problems

1πŸ‘

βœ…

It is because the console log likely happened long before your then block executed. Its initial value is an array of four integers before you overwrite it with a length. Try making the created function async and await the axios promise chain to resolve.

async function created() {
  await axios.get('http://localhost:3030/disruptions/', { // await the resolve
    params: {
      DisruptionCategory: 0
    }
  })
  .then((response) => {
    this.disturbances_category_0 = response.data.data; //HERE IS THE COMPLETE ARRAY 
    this.datasets[0].data[0] = this.disturbances_category_0.length; //HERE I WANT TO SET THE LENGTH
  })
  .catch((error) => {
    console.log(error.data);
  });

  //imagine that for the other fruits as well...
  console.log(this.datasets[0].data[0]); // now this should be updated
}
πŸ‘€Drew Reese

0πŸ‘

console.log(this.datasets[0].data[0]); 

The above will run before the response to your request has been handled since it is asynchronous. Your code will just keep executing while the .then() part will execute on another thread once you get a response from the server.

πŸ‘€Jonas B

Leave a comment