I am getting this error:
Property 'data' does not exist on type 'Promise<void | AxiosResponse<{ data: { email: string; username: string; }; }, any>>'.ts(2339)
while I do the post request like this:
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;
};
how to handle this? any help please?
CodePudding user response:
Put await
keyword before axios.post
.
Then get the data like this :
const data = res && res.data
This is because axios
is async
and needs to be awaited to fetch the data.