Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'data' does not exist on type 'Promise<void | AxiosResponse>

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading