[Vuejs]-How to make multiple requests and save the data objects into a new array

0👍

Try this:

getAnträgeByBenutzerID(){
    this.test = [];
    const promises = [];
    this.benutzerIDs.forEach(id => {
        promises.push(axios.get(server.baseURL + "/urlaubsantrag/benutzer/" + id))
    })

    axios
        .all(promises)
        .then(res =>
            //res is an array of raw data
            this.test = res.map(item => item.data);
         )
}

0👍

You’ll need to use Promise.all and Array.prototype.map:

getAnträgeByBenutzerID(){
  return Promise.all(this.benutzerIDs.map((id) =>
     axios.get(server.baseURL + "/urlaubsantrag/benutzer/" + id)])));
}

The map callback creates an array of promises which are then collectively returned once they are all resolved.

Leave a comment