我一直在让getStaticPaths和getStaticProps发挥作用,但不知怎么的,我觉得我错过了什么。我试着在每一个里面查询,我认为这是重复的,但我不知道该怎么做。有人能给我举个例子吗?
这是我的代码:
export async function getStaticProps() {
// Call an external API endpoint to get posts.
// You can use any data fetching library
const { data, error, isLoading } = useMoralisQuery(
"Posts",
(query) => query.equalTo("objectId", postObjID),
[postObjID]
);
const postDetailstringfied = JSON.parse(JSON.stringify(data));
// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
postDetailstringfied,
},
};
}
export async function getStaticPaths() {
// When this is true (in preview environments) don't
// prerender any static pages
// (faster builds, but slower initial page load)
if (process.env.SKIP_BUILD_STATIC_GENERATION) {
return {
paths: [],
fallback: "blocking",
};
}
// Call an external API endpoint to get posts
const { data, error, isLoading } = useMoralisQuery(
"Posts",
(query) => query,
[]
);
const postDetailstringfied = JSON.parse(JSON.stringify(data));
// Get the paths we want to prerender based on posts
// In production environments, prerender all pages
// (slower builds, but faster initial page load)
const paths = postDetailstringfied.map((post) => ({
params: { id: post.id },
}));
// { fallback: false } means other routes should 404
return { paths, fallback: false };
}
react钩子在服务器端不起作用。你可以使用这里提供的香草js语法
在你的情况下,替代方案是
const Posts = Moralis.Object.extend("Posts");
const query = new Moralis.Query(Posts);
query.equalTo("objectId", postObjID);
const results = await query.find();