首页 > 解决方案 > 无法在 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}`);
  }
};

任何想法?谢谢!!

标签: javascriptreactjsnext.js

解决方案


我以前多次看到这个错误。

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 状态


推荐阅读