[Vuejs]-Updating array afte doing a post request

0👍

It is because this.getActors(); is called before the fetch method is done. That’s the way javascript works.

You can try putting this.getActors(); in the then.

Example :

if (this.newActor.firstName != '' && this.newActor.lastName != '') 
    {
      let self = this
      fetch(this.urlBase+'actors/add', {
      method: 'POST',
      body: JSON.stringify(this.newActor),
      headers:{
        'Content-Type': 'application/json'
      }
      })
      .then(res => res.json())
      .catch(error => console.error('Error:', error))
      .then(response => {
        console.log('Success:', response); 
        this.alert = true;
        this.alertMessage = "Usuario agregado con éxito"; 
        this.getActors(); //Here is where I tried to get the actors again.
        console.log(this.actors);
      })
    }

Leave a comment