Async Function returning Undefined when calling it

I'm Having some trouble with this function on a React project

let fetchData = async (event, imagen) => {
    const apiURL = `https://some_api_call/${imagen}`;
    await axios.get(apiURL).then((response) => {
      console.log(response.data.Imagen1.data);
      return response.data.Imagen1.data;
    });

When i call it the Console.log returns Undefined but the console log above returns the data

fetchData(event, rowData.Codigo).then(function (response) {
console.log(response);
});

Upvotes: 0

Views: 52

Answers (2)

Bergi
Bergi

Reputation: 664297

Your fetchData function has no return statement. You probably don't want to use then when there is await:

async function fetchData(event, imagen) {
  const apiURL = `https://some_api_call/${imagen}`;
  const response = await axios.get(apiURL)
  console.log(response.data.Imagen1.data);
  return response.data.Imagen1.data;
}

Upvotes: 1

The Bomb Squad
The Bomb Squad

Reputation: 4337

let fetchData = async (event, imagen) => {
    const apiURL = `https://some_api_call/${imagen}`;
    return await axios.get(apiURL);
}
fetchData(event, rowData.Codigo).then(function (response) {
    console.log(response);
});

Upvotes: 1

Related Questions