2

我正在尝试过滤所有没有类别的产品(这是参考数组)并且找不到如何做到这一点。

S.listItem()
  .title('Without category')
  .id('productsWithoutCategories')
    .child(
      S.documentList()
        .title('Without categories')
        .menuItems(S.documentTypeList('product').getMenuItems())
        .filter('_type == $type && !category')
        .params({ type: 'product' })
      )

!category不工作。我将不胜感激。

4

2 回答 2

3

使用 Vision 找到答案(非常令人愉快的工具)

https://www.sanity.io/docs/the-vision-plugin

 .filter('_type == $type && !defined(categories)')
 .params({ type: 'product' })
于 2020-02-18T05:42:23.493 回答
2

正如您自己已经发现的那样,您可以使用该defined函数检查是否设置了某些内容(或使用 NOT 运算符未设置!)。defined您可以在此处找到有关该功能的一些文档:

只想从文档中引用:

您永远不会在存储在 Sanity 中的文档上看到空值。如果将值更新为 null,则该键将消失。因此,您可能从 SQL 中知道的 is null 约束在这里没有任何意义。值是defined!defined

从查询工作方式页面:

通过字段的存在进行过滤,例如*[defined(status)],仅匹配状态属性设置为任何值的文档。

于 2020-02-18T11:42:28.767 回答