【发布时间】:2022-02-07 20:27:43
【问题描述】:
export const getServerSideProps = async ({ params }) => {
const res = await fetch(
`https://pixabay.com/api/?key=${process.env.API_KEY}&id=${params.id}`
);
const { hits } = await res.json();
if (!hits) {
return {
notFound: true,
};
}
return {
props: { pic: hits[0] },
notFound: true,
};
};
每当用户输入无效的 params.id=====>
时,我都会不断收到此错误服务器错误 FetchError:https://pixabay.com/api/?key=*************************************&id=**xzk** 处的无效 json 响应正文:JSON 中位置 1 处的意外令牌 E
我使用“xzk”作为无效的 id 值,而不是将用户重定向到 notFoundPage,它一直显示此错误,并且该错误甚至显示了我的 api-keys。请问我做错了什么?想法也很受欢迎。提前致谢。
【问题讨论】:
标签: javascript node.js reactjs react-native next.js