“Promise”类型上不存在属性“data”<void | AxiosResponse>

问题描述 投票:0回答:1

我收到此错误:

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;
  };

我该如何处理这个问题?

reactjs typescript axios
1个回答
1
投票

await
关键字放在
axios.post
之前。 然后获取这样的数据:

const data = res && res.data

这是因为

axios
async
并且需要等待才能获取数据。

© www.soinside.com 2019 - 2024. All rights reserved.