我想使用从params
中获取的getStaticPaths
来获取内容中的数据。然而,我没有得到正确的参数,所以我得到了错误的数据。
“string”类型的
参数不能分配给类型为“{ slug: string;}”的参数。
const getBlogEntries = async () => {
const { items }: EntryCollection<IBlogFields> = await client.getEntries({
content_type: "blog"
})
return items
}
const getBlogEntry = async ({ slug }: { slug: string }) => {
console.log('slug', slug)
const { items }: EntryCollection<IBlogFields> = await client.getEntries({
content_type: "blog",
"fields.slug[in]": slug,
limit: 1
})
return items
}
export const getStaticPaths: GetStaticPaths = async () => {
const items = await getBlogEntries()
const paths = items.map((item) => {
return {
params: { slug: item.fields.slug },
}
})
return {
paths,
fallback: false,
}
}
export const getStaticProps: GetStaticProps = async ({
params,
}) => {
console.log('params', params!.slug)
const items = await getBlogEntry(params!.slug as string)
// the error occurs in `params!.slug`
return {
props: {
blog: items[0],
},
}
}
发布于 2022-05-05 00:36:44
我有个数据:
const items = await getBlogEntry({ slug: params!.slug})
https://stackoverflow.com/questions/72124223
复制