【发布时间】:2023-01-31 23:48:35
【问题描述】:
在创建帖子(用于博客)时使用编辑器,我曾经直接将它的输出(html string)保存到mongo中。
然后在添加 SSG 之后,在构建时,(控制台)获取的数据显示为 this。
而只需获取 api 即可正确显示数据。 here
代号获取静态道具&获取静态路径
export async function getStaticProps({ params }) {
try {
const { data } = await axios.post(baseUrl + getPostBySlug, { slug: params?.slug });
console.log({ slug: params?.slug }, 'data 2 ->', data); // here is the data consoled
return {
props: { post: data?.data ?? null },
revalidate: 10,
}
}
catch (err) {
return {
props: { post: null },
revalidate: 10,
}
}
}
export async function getStaticPaths() {
try {
const res = await fetch(baseUrl + getAllPosts, { method: 'GET' });
const data = await res?.json();
if (data?.success && data?.data) {
return {
paths: data?.data?.map(({ slug }) => ({ params: { slug } })),
fallback: true,
}
}
else {
return {
paths: [{ params: { slug: '/' } }],
fallback: true,
}
}
}
catch (err) {
return {
paths: [{ params: { slug: '/' } }],
fallback: true,
}
}
}
最终输出,一个 SSG 页面但没有数据初始化 -> here
【问题讨论】:
-
在返回之前尝试在你的
getStaticProps内console.log(data?.data) -
@AhmedSbai 是的,我在 getStaticProps 的第二行安慰了它。
标签: next.js static-site-generation