首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何从理智中获取数据?

我如何从理智中获取数据?
EN

Stack Overflow用户
提问于 2022-06-16 23:53:04
回答 1查看 268关注 0票数 0

我试图创建一个模式并从sanity中获取数据。但是控制台日志显示未定义。我在这里做错什么了?我可能在创建模式方面失败了,但我不确定。

Testimonials.tsx

代码语言:javascript
复制
interface Props {
    testimonial: [Testimonial]
}

const Testimonials = ({ testimonial } : Props) => {
    console.log(testimonial)
 return ( 
<div className="testimonials_info">
                    <h5>TESTIMONIALS</h5>
</div>
)
}

导出getserverside道具

代码语言:javascript
复制
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。

代码语言:javascript
复制
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

代码语言:javascript
复制
export interface Testimonial {
    _id: string;
    _createdAt: string;
    title: string;
    description: string;
    author: string;
    slug: {
        current: string;
    };
    body: [object];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-17 00:01:08

看起来您的查询是在寻找“证明”类型,但是模式的名称是“证明”。将其改为以下内容应该有效:

代码语言:javascript
复制
const query = `*[ _type == "testimonials"] {
        title,
        slug,
        description,
    }`;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72652997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档