我收到此错误:
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;
};
我该如何处理这个问题?
将
await
关键字放在 axios.post
之前。
然后获取这样的数据:
const data = res && res.data
这是因为
axios
是async
并且需要等待才能获取数据。