在vercel上部署Next.js时的错误:位置0的JSON中的意外令牌r

发布于 2025-01-23 05:36:04 字数 1188 浏览 3 评论 0原文

我正在从API中获取JSON数据,如以下

页面/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)

I am fetching json data from api like below

-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()

It's a bit long length of output. I am not sure that's the reason, but fails while deploying on 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2025-01-30 05:36:04

我通过添加捕获量避免错误。我找不到更好的方法。

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

I avoid error by adding catch. I can't find better way.

try{
    const json = await charity.json()
} catch(err) {
    console.log("err: ", err)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文