Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Response Data is undefined for async map function inside async function

I am trying to fetch data from an API and modify the response from API but I am getting the modified response as undefined.

export const fetchData = async () => {
  
  const response = await fetch(`https://api/`, options);

  const results = await response.json();

  let responseData = await Promise.all(results.map(async (result) => {
    const response = await fetch(
      `https://another_api_endpoint/${result.id}`,
      options
    );

    const data = await response.json();

    result.newProperty = data.propertyTOAdd;

  }));

  console.log("Response Data:- ", responseData);

};

console.log()

Response Data:- [
  undefined, undefined,
  undefined, undefined,
  undefined, undefined,
  undefined, undefined,
  undefined, undefined
]

What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you are not returning anything from map

try to change your code in this way

export const fetchData = async () => {
  
  const response = await fetch(`https://api/`, options);

  const results = await response.json();

  let responseData = await Promise.all(results.map(async (result) => {
    const response = await fetch(
      `https://another_api_endpoint/${result.id}`,
      options
    );

    const data = await response.json();

   return {...result, newProperty: data.propertyTOAdd};

  }));

  console.log("Response Data:- ", responseData);

};


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading