简体   繁体   English

异步 Function 调用时返回未定义

[英]Async Function returning Undefined when calling it

I'm Having some trouble with this function on a React project我在 React 项目中遇到了这个 function 的问题

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当我调用它时,Console.log 返回 Undefined 但上面的控制台日志返回数据

fetchData(event, rowData.Codigo).then(function (response) {
console.log(response);
});
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);
});

Your fetchData function has no return statement.您的fetchData function 没有return语句。 You probably don't want to use then when there is await :当有await时,您可能不想使用then

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM