我正在寻找我在这段代码中做错了什么,但 y 无法解决它。 我需要在getInitialPops中使用我的 query.id 来填充一些内容。获取有效,但在我的捕获中,我收到此错误:
FetchError: invalid json response body at https://url/resume-true.json reason: Unexpected token < in JSON at position 0
原始端点是:
https://url/resume-${query.id}.json
当我安慰时.log query.id 首先返回
slug-5 (this is the rigth one)
第二:
true (I dont'know were this came from)
这是我的getInitialProps
channelRouter.getInitialProps = async ({ query }) => {
console.log("query**", query.id);
try {
const res = await fetch(
`https://url/resume-${query.id}.json`
);
let data = await res.json();
return { data };
} catch (e) {
console.log(`${e}`);
}
};
知道吗? 谢谢!!
我以前见过很多次这个错误。
FetchError: invalid json response body at https://url/resume-true.json reason: Unexpected token < in JSON at position 0
这意味着您的端点返回的是 HTML 而不是 JSON(因此以<
开头(
检查您的终端节点,它不是有效的 json,可能是 html 中的 404 或 500 状态