gpt4 book ai didi

Error: Failed to collect page data from /blog/[id](错误:无法从/blog/[id]收集页面数据)

转载 作者:bug小助手 更新时间:2023-10-26 20:24:46 28 4
gpt4 key购买 nike



const Details = ({ userData }) => {
return (
<Layout>
<h1>{userData?.header}</h1>
</Layout>
)
}

export default Details


export const getStaticPaths = async () =>{
const res = await fetch(`http://localhost:3000/api/blogs`);
const blogs = await res.json();
const ids = blogs.map((blog) => blog.id);
const paths = ids.map((id)=> ({params:{id: id.toString()}}));

return{
paths,
fallback:false,
};
};

export const getStaticProps = async (context) =>{
const res = await fetch(`http://localhost:3000/api/blogs/${context.params.id}`);
const userData = await res.json();

return{
props:{
data,
userData,
}
}
}

While npm run build my Next.js project, I am getting such an error in the console:

当NPM运行Build My Next.js项目时,我在控制台中收到以下错误:


Error: Failed to collect page data from /blog/[id]

Why does this happen?

这一切为什么要发生?


更多回答

Before asking question, it may be beneficial to take the tour and read the various pages in the help center, particularly How to Ask or Why should I not upload images of code/data/errors?.

在提问之前,最好先浏览并阅读帮助中心的各个页面,特别是如何提问或为什么我不应该上传代码/数据/错误的图像?

configuring similar errors, I imported my own data.js file as import {data}. but ı didn't fix my mistake. help me please.

配置类似的错误,我将自己的data.js文件导入为import {data}。但他没能弥补我的错误请帮帮我。

This is during build time right?

这是在构建时进行的,对吗?

Can this help you: github.com/vercel/next.js/discussions/…

这对你有帮助吗:github.com/vercel/next.js/discussions/.

unfortunately i saw this and tried it but nothing changed.

不幸的是,我看到了这个,并尝试了它,但没有任何改变。

优秀答案推荐

In my case it was because I had a import of a module at the top like import { pusherServer } from '@/lib/pusher';. It is asynchronous. I moved the import inside the GET request function it was supposed to be called in, and did const { pusherServer } = require('@/lib/pusher');, which is synchronous and waits for it to load. This fixed it. Locally I had no problem building but when deploying on my VPS I had the error message of "Error: Failed to collect page data for {route}" pop up.

在我的例子中,这是因为我在顶部有一个模块的导入,比如import { pusherServer } from '@/lib/pusher';。它是异步的。我将导入移到了GET请求函数中,并执行了const { pusherServer } = require('@/lib/pusher');,这是同步的,并等待它加载。这解决了它。在本地我没有问题的建设,但当部署在我的VPS我有错误消息“错误:未能收集页面数据为{route}”弹出。


edit: The reason I had a problem deploying on the VPS was because I was using coolify and the option of injecting the secrets during buildtime was turned off. Therefore the asynchronous way of doing "import from @/lib/pusher" was not working. After enabling it I was able to use the import method instead of require(). What require() did was inject the secret during runtime, not buildtime

编辑:我在VPS上部署时遇到问题的原因是因为我正在使用Coolify,而在构建时注入机密的选项被关闭了。因此,“从@/lib/pusher导入”的异步方式不起作用。在启用它之后,我能够使用导入方法而不是Required()。Required()所做的是在运行时注入秘密,而不是在构建时



Because of you are not fetching anything from any Api, you don't need to fetch anything remotly. That's why you are getting an ECONNREFUSED error.

因为您没有从任何Api获取任何东西,所以您不需要远程获取任何东西。这就是您收到ECONNREFUSED错误的原因。


Update


I've found two errors in your /blog/* folder:

我在您的/Blog/*文件夹中发现了两个错误:



  • Add - 1 in line 106 of /blog/[id].js (See bellow)

  • Remove lines 131 and 132 of /blog/index.js


/blog/[id].js

/博客/[id].js


import { data } from "../../data.js";

const Details = ({ userData }) => {
return <Layout>{/* ... */}</Layout>;
};

export default Details;

export const getStaticPaths = async () => {
const ids = data.map(({ id }) => id);
const paths = ids.map((id) => ({ params: { id: id.toString() } }));
return {
paths,
fallback: false,
};
};

export const getStaticProps = async (context) => {
// You where trying here to get posts by id:
// 1, 2, 3, 4
// But what you really want is to get array indexes:
// 0, 1, 2, 3
// that's why we are adding - 1
const userData = data[context.params.id - 1]; // <----- add - 1

return {
props: {
data,
userData,
},
};
};

/blog/index.js

/博客/index.js


export const getStaticProps = async (context) => {
// ----- remove the fetch -----

return {
props: {
data,
},
};
};

更多回答

Can you please look at the photo I added last time, it gives these errors while downloading your codes and now while building.

请你看看我上次添加的照片,它在下载你的代码时和现在构建时都会给出这些错误。

If you solve it, don't get me wrong, but I want to hug you, I've been dealing with this for 3 days. :) picture at the top.

如果你解决了,不要误会,但我想拥抱你,我已经处理了3天了。:)顶部的图片。

Did something change with my code?

我的代码有什么变化吗?

no.I did copy paste.

不,我复制粘贴了。

If you come to Turkey on vacation, you can definitely contact me and I will help you with everything. :)

如果你来土耳其度假,你一定可以联系我,我会帮助你的一切。:)

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com