簡體   English   中英

異步 Function 調用時返回未定義

[英]Async Function returning Undefined when calling it

我在 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;
    });

當我調用它時,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);
});

您的fetchData function 沒有return語句。 當有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