【问题标题】:GROQ: Query one-to-many relationship with parameter as query inputGROQ:以参数为查询输入查询一对多关系
【发布时间】:2023-02-02 16:13:30
【问题描述】:

我有一个用 NextJS 构建的博客,由 Sanity 提供支持。我想开始使用标签/类别来标记帖子。

每个帖子可能有很多类别。

类别是帖子的参考:

 defineField({
   name: 'category',
   title: 'Category',
   type: 'array',
   of: [
     {
       type: 'reference',
       to: [
         {
           type: 'category',
         },
       ],
     },
   ],
 }),

这是我的 GROQ 查询:

*[_type == "post" && count((category[]->slug.current)[@ in ['dogs']]) > 0] {
  _id,
  title,
  date,
  excerpt,
  coverImage,
  "slug": slug.current,
  "author": author->{name, picture},
  "categories": category[]-> {name, slug}
}

上面的代码是硬编码的,但是用 $slug 换出 'dogs' 会导致查询失败。 (其中 $slug 是提供的参数)

{
  $slug: 'travel' 
}

如何使上述动态?

【问题讨论】:

    标签: sanity groq


    【解决方案1】:

    返回用户提供的 currentLocation 参数 10 英里范围内的所有店面文档; // 对于给定的 $currentLocation 地理点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-13
      • 2019-02-02
      • 2014-07-14
      • 2015-03-31
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多