passing response data to an array

May be you would need to move map inside to the callback

let users = [];

const fetchData = () => {
  axios.get("https://reqres.in/api/users").then(({ data }) => {
    users = data.data;

    const mapUsers = users.map((item) => {
      return {
        avatar: item.avatar,
      };
    });
    console.log("mapUsers", mapUsers);
  });
};

fetchData();

or

let users = [];

const fetchData = () => {
  return axios.get("https://reqres.in/api/users").then(({ data }) => {
    return data.data;
  });
};

fetchData().then((data) => {
  console.log(data);
  const mapUsers = data.map((item) => {
    return {
      avatar: item.avatar,
    };
  });

  console.log("mapUsers", mapUsers);
});

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top