我试图创建一个模式并从sanity中获取数据。但是控制台日志显示未定义。我在这里做错什么了?我可能在创建模式方面失败了,但我不确定。
Testimonials.tsx
interface Props {
testimonial: [Testimonial]
}
const Testimonials = ({ testimonial } : Props) => {
console.log(testimonial)
return (
<div className="testimonials_info">
<h5>TESTIMONIALS</h5>
</div>
)
}导出getserverside道具
export const getServerSideProps = async () => {
const query = `*[ _type == "testimonial"] {
title,
slug,
description,
}`;
const testimonials = await sanityClient.fetch(query)
if (!testimonials.length) {
return {
props: {
testimonials: [],
},
}
} else {
return {
props: {
testimonials,
},
}
}
}testimonials.js (模式)我构建它是随机的,我不确定我是否把它传递给getserversideprops。
export default {
name: "testimonials",
title: "Testimonials",
type: "document",
fields: [
{
name: "title",
title: "Title",
type: "string",
},
{
name: "publishedAt",
title: "Published at",
type: "datetime",
},
{
name: "body",
title: "Text",
type: "blockContent",
},
],
preview: {
select: {
title: "testimonials",
},
},
};typing.d.tsx
export interface Testimonial {
_id: string;
_createdAt: string;
title: string;
description: string;
author: string;
slug: {
current: string;
};
body: [object];
}发布于 2022-06-17 00:01:08
看起来您的查询是在寻找“证明”类型,但是模式的名称是“证明”。将其改为以下内容应该有效:
const query = `*[ _type == "testimonials"] {
title,
slug,
description,
}`;https://stackoverflow.com/questions/72652997
复制相似问题