axios在提交表单时抛出undefined,但在第二次按下时抛出数据



所以这是我的问题。使用异步函数时,我的响应会抛出未定义的响应,但当我第二次提交时,会抛出正确的响应。这是我的功能:

let showcode = async (data) => {
try {
setLoading(true);
const url = `https://fortnite-api.com/v2/creatorcode?name=${data.code}`;
let res = await axios.get(url);
setInfo(res);
console.log(info);
setLoading(false);
} catch (err) {
setLoading(false);
console("error aca "+ err);
}
};

当您尝试使用尚未完全检索到的数据时,通常会发生这种情况。您应该先等待数据,然后再尝试使用它。

相关内容

最新更新