我收到此錯誤:
Property 'data' does not exist on type 'Promise<void | AxiosResponse<{ data: { email: string; username: string; }; }, any>>'.ts(2339)
當我像這樣進行發布請求時:
const sendRequest = async (params: LoginProps) => {
const res = axios
.post<{ data: { email: string; username: string } }>(
'http://localhost:3333',
{
...params,
}
)
.catch((err) => console.log('errors', err));
const data = await res.data; //error is here
return data;
};
如何處理?請問有什么幫助嗎?
uj5u.com熱心網友回復:
把await關鍵字放在前面axios.post。然后得到這樣的資料:
const data = res && res.data
這是因為axios需要async等待以獲取資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/514943.html
標籤:反应打字稿axios
上一篇:如何迭代模板文字聯合型別?
下一篇:從陣列構造物件并實作tab補全
