Skip to content Skip to sidebar Skip to footer

Axios Promise Returns Correct Value In "axios.all" Function, But Is Undefined In The "then" Function

I'm following a tutorial in which data from the git API is requested and a scoring algorithm will order that data. The battle function will take an array of two elements, i.e two g

Solution 1:

Your getUserData function does not return anything. Change it as below:

functiongetUserData(player) {
  return axios.all([
  // ...
  ]);
}

That behaviour is because you return an array of undefined values when you do response.map where you replace all the items with undefined (console.log returns undefined).

Instead, return the actual result from the asynchronous call:

module.exports = {
  battle: function(players) {
      return axios.all(players.map(getUserData))
        .then(response => {
            response.forEach(each =>console.log(each));
            return response;
        });
  }
}

Post a Comment for "Axios Promise Returns Correct Value In "axios.all" Function, But Is Undefined In The "then" Function"