[Vuejs]-Array.foreach wont work. Sure im missing something small

3👍

You need to run this forEach code block after the API request completes, so inside the then of your api call promise. That way you will be waiting for the async api request to complete and if it completes with success, probably an HTTP 200 or alike, you run your code to update the state array. Asynchronous processes like most remote api requests should be waited if you want to use their response.

You may also consider using a catch block to handle the error cases too.

else {
        axios.post('/app/planningtool/files/upload',
          formData,
          {
            headers: {
              'Content-Type': 'multipart/form-data'
            }
          })
          .then(response => {
             this.newIndexes = response.data

             this.newIndexes.forEach((value, i) => {
               console.log('hi')
               console.log(value);
               let IndexToEdit = this.indexOfAddedFile + i;
               console.log(IndexToEdit);
               this.allData[parseInt(IndexToEdit)].ID = parseInt(value);
             });

        });

        this.allData = this.allData.concat(this.allDataNew);
    }

Leave a comment