【问题标题】:Getting issue while fetching prev/next post from Sanity CMS ( Next js +Sanity static blog)从 Sanity CMS 获取上一篇/下一篇文章时出现问题(Next js +Sanity 静态博客)
【发布时间】:2022-11-13 16:50:27
【问题描述】:

我正在使用 Sanity CMS,并尝试使用下面的查询获取上一篇和下一篇文章。

  const query = `*[_type=='post' && slug.current == $slug][0]{
    _id,title,_updatedAt,
    "newPost" : *[_type == 'post' && ^._updatedAt > _updatedAt][0] {
     title,"slug" : slug.current
   }
}`;
  const param = {
    slug: "some-random-slug",
  };
  const nextPrev = await client.fetch(query, param);

我的问题是,当我在 Sanity Vision 尝试上述查询时,它运行良好。但是在使用健全客户端尝试此操作时,它不会返回 newPost 对象。我怎么了?

【问题讨论】:

    标签: content-management-system next sanity headless-cms


    【解决方案1】:

    尝试这个:

    const query = `*[_type == "post" && slug.current == $slug][0] {
      "nextPost": *[_type == "post" && ^._createdAt < _createdAt] | order(_createdAt asc)[0] {
       // Fields
      }
    }`
    
    const { nextPost } = await client.fetch(query, { slug })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 2015-06-26
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2021-07-23
      相关资源
      最近更新 更多