[英]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.