javascript - 无法在 nextJS 中使用 query.ID 生成内容
问题描述
我正在寻找我在这段代码中做错了什么,但你无法解决它。我需要在 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
当我 console.log 查询.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 状态
推荐阅读
- java - 使用 android 复选框崩溃
- swift - Swift UI 按钮动作函数
- java - MCP 无法识别的选项:-Xincgc
- python - 'geckodriver' 可执行文件需要通过 Selenium 使用 GeckoDriver 和 Firefox 在 PATH 中
- c# - 用户定义的从接口到类的转换
- twitter-bootstrap - Bootstrap 5 中的多级下拉菜单
- javascript - 使用 Node.js 同时接受文件和文本输入
- r - 在数据表的单元格中显示多个字符串,可以通过单击它们来删除这些字符串
- parallel-processing - 使用 CUDA/thrust 在数组中设置每个 float4 的一个元素
- minecraft - 如果半径内有某个块如何执行