reactjs - 在 vercel 上部署 Next.js 时出错 : Unexpected token R in JSON at position 0

标签 reactjs json next.js fetch vercel

我正在从 api 获取 json 数据,如下所示

-pages/explorer.js

    const charity = await fetch("https://api.www.every.org/api/search_v0?query=&causes="+categories[i]+"&take=100&skip=0", {
        method: `GET`,
        headers: {
            Accept: "application/json"
        }
    });
    const json = await charity.json()

输出的长度有点长。我不确定这就是原因,但在 vercel 上部署时失败。

Error occurred prerendering page "/explorer". Read more: https://nextjs.org/docs/messages/prerender-error
FetchError: invalid json response body at https://api.www.every.org/api/search_v0?query=&causes=coronavirus&take=100&skip=0 reason: Unexpected token R in JSON at position 0
    at /vercel/path0/node_modules/node-fetch/lib/index.js:272:32
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async getStaticProps (/vercel/path0/.next/server/pages/explorer.js:331:18)
    at async renderToHTML (/vercel/path0/node_modules/next/dist/server/render.js:329:24)
    at async /vercel/path0/node_modules/next/dist/export/worker.js:273:36
    at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:60:20)

最佳答案

我通过添加 catch 来避免错误。我找不到更好的方法。

try{
    const json = await charity.json()
} catch(err) {
    console.log("err: ", err)
}

关于reactjs - 在 vercel 上部署 Next.js 时出错 : Unexpected token R in JSON at position 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71963792/

相关文章:

node.js - 无法在 nextjs 的 getServerSideProps 中访问 session 或 req.user

reactjs - React-Admin:如何发送通过 API 调用自动填充的输入值?

Reactjs 更新状态中数组中的单个元素

google-app-engine - Google App Engine 标准环境与 next/image 兼容吗?

json - 如何在 JsonNode 中创建插入新节点?

python - Python 重新编码 JSON 文件

typescript - 使用 next.js 13 连接到 Upstash 时无法读取未定义的属性(读取 'startsWith' )

javascript - 在reactjs中的react.Component中异步AJAX请求后无法获取

javascript - DRY React 编写多个类似的函数

android - 异步任务中的空指针错误