【发布时间】:2022-01-03 07:46:17
【问题描述】:
我将使用 Axios 来通信 API。 但是这种错误不断出现。我不明白这个问题。我在互联网上搜索并尝试了一切。帮我。 我只想点击那个按钮来查看开发者工具中的低价值。
useEffect(() => {
setJwt(getClientCookieFromClient('jwt'));
}, []);
const customFetch = async () => {
const res = await axios
.get(`${process.env.NEXT_PUBLIC_WECODE_URI}/subscription/master_table`, {
headers: {
Authentication: jwt,
},
})
.then((res) => res.data);
if (!res.data.success) {
alert(res.data.message);
}
};
...
<button onClick={() => customFetch()}>API호출버튼</button>
【问题讨论】:
-
在
.then行之后,添加.catch(e) {console.error(e)}。这将通过记录错误来处理错误,并有望提供有关问题所在的更多详细信息。 -
remove ".then((res) => res.data);",该部分将“data”分配给“res”,这使您的 if 语句失败。
-
@user2740650 语法为
.catch(e => console.error(e))
标签: javascript reactjs typescript next.js