我的应用程序中有一个我似乎无法解决的错误。
我用 axios
与 TypeScript
.
这是我尝试做的代码示例:
export const fetchTransactions = (PageNum: number, PageSize: number, Context_id: number) => new Promise<Transaction[]> (async (resolve, reject) => {
try
{
const response = await axios.post<AxiosResponse<Transaction[]>>(FETCH_TRANSACTIONS_URL, {PageNum, PageSize, Context_id})
const {transactions} = response.data
resolve(transactions)
}
catch (error) {
reject(error.response);
}
})
现在我得到的错误 const {transactions} = response.data
如下:我怎样才能消除这个错误?正确的响应类型应该是什么?
最佳答案
通常,我以这种方式将 axios 与 Typescript 一起使用
const fetchTransactions = (PageNum: number, PageSize: number, Context_id: number): Promise<Transaction[]> =>
axios
.post<Transaction[]>(FETCH_TRANSACTIONS_URL, {PageNum, PageSize, Context_id})
.then((response) => {
if (response.status >= 200 && response.status < 300) {
return response.data;
}
throw new Error(response.status.toString());
})
.catch(({ response }) => {
throw new Error(response.status);
});
关于typescript - 在 TypeScript 中使用 Axios 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66477636/