【发布时间】:2022-08-19 19:59:32
【问题描述】:
我遇到了一个奇怪的问题,我无法在其中定义了 try catch 块的匿名函数中定义变量。
let response: AxiosResponse<CustomType[]>; // had to define outside the useQuery
const { data: info } = useQuery(
[\'queryKey\', a, b],
async () => {
// let response: AxiosResponse<CustomType[]>; //ERROR variable response is used before being assigned
try {
response = await getAxios().get(`requestURL`);
const responseFiltered = {};
response.data.forEach((a) => {
responseFiltered[a] = a;
})
return responseFiltered;
} catch (error) {
logger.error({
meta: { error, response}, // variable used here
});
}
}
);
不知道为什么它期望在 useQuery 函数之外定义响应变量。
-
你得到什么样的错误?这是来自 eslint,还是来自打字稿,还是在运行时?此外,您在不存在的 catch 块中使用
responseFiltered。 -
@TkDodo 感谢您指出这一点。我的意思是在我现在更新的 catch 块中输入它作为响应。这是打字稿错误
variable response is used before being assigned
标签: javascript try-catch react-query