通过对象数组[Sanity.io&GROQ]

时间:2019-10-17 13:29:39

标签: sanity groq

我正在尝试在我的产品列表中找到一个产品变体(使用GROQ在sanity.io上),为此,我有想要的变体的sku。

我正在使用的查询是 *[_type == "product" && variants[].sku.current =="kit-kat-wasabi-5" ] 但是此查询返回一个空数组。我确信该sku是正确的,因为如果我将过滤器放在一边,然后获取所有我能找到的过滤器。 我尝试用match替换“ ==”,但是结果是一样的。

我的模式是

过程

export default {
  name: 'product',
  title: 'Product',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'Inner Title',
      type: 'string'
    },
    {
      title: 'SKU',
      name: 'sku',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      },
      validation: Rule => Rule.required()
    },
    {
      name: 'titleWebsite',
      title: 'Title Website',
      type: 'localeString'
    },
    {
      name: 'active',
      title: 'Active',
      type: 'boolean'
    },
    {
      name: 'mainImage',
      title: 'Imagem',
      type:"image"
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      }
    },
    {
      title: 'Base Price',
      name: 'basePrice',
      type: 'localeCurrency'
    },
    {
      title: 'Quantidade',
      name: 'qty',
      type: 'number'
    },
   /* {
      title: 'Default variant',
      name: 'defaultProductVariant',
      type: 'productVariant'
    },*/
    {
      title: 'Variants',
      name: 'variants',
      type: 'array',
      of: [
        {
          title: 'Variant',
          type: 'productVariant'
        }
      ]
    },
    {
      title: 'Tags',
      name: 'tags',
      type: 'array',
      of: [
        {
          type: 'string'
        }
      ],
      options: {
        layout: 'tags'
      }
    },
    {
      name: 'vendor',
      title: 'Vendor',
      type: 'reference',
      to: {type: 'vendor'}
    },
    {
      name: 'blurb',
      title: 'Blurb',
      type: 'localeString'
    },

    {
      name: 'categories',
      title: 'Categories',
      type: 'array',
      of: [
        {
          type: 'reference',
          to: {type: 'category'}
        }
      ]
    },
    {
      name: 'body',
      title: 'Body',
      type: 'localeBlockContent'
    }
  ],

  preview: {
    select: {
      title: 'title',
      manufactor: 'manufactor.title',
      media: 'mainImage'
    }
  }
}

和productVariant

export default {
  title: 'Product variant',
  name: 'productVariant',
  type: 'object',
  fields: [
    {
      title: 'Title',
      name: 'title',
      type: 'string'
    },
    {
      title: 'Title Website',
      name: 'titleWebsite',
      type: 'localeString'
    },
    {
      title: 'Weight in grams',
      name: 'grams',
      type: 'number'
    },
    {
      title: 'Price',
      name: 'price',
      type: 'localeCurrency'
    },
    {
      title: 'SKU',
      name: 'sku',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 96
      },
      validation: Rule => Rule.required()
    },
    {
      title: 'Taxable',
      name: 'taxable',
      type: 'boolean'
    },
    {
      name: 'blurb',
      title: 'Blurb',
      type: 'localeString'
    },
    {
      name: 'images',
      title: 'Images',
      type: 'array',
      of: [
        {
          type: 'image',
          options: {
            hotspot: true
          }
        }
      ]
    },
    {
      title: 'Quantidade',
      name: 'qty',
      type: 'number'
    },
    {
      title: 'Bar code',
      name: 'barcode',
      type: 'barcode'
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

这是遍历数组时GROQ中的一个已知错误。如果要在GROQ v1中进行修复,将会引入重大更改。因此,它将在GROQ v2中修复。

以下是错误报告,解释了该问题:https://github.com/sanity-io/sanity/issues/1557。您可以在这里表达对这个问题的兴趣。

此处有第二版的工作草案:https://github.com/sanity-io/groq

关于您的架构,我会考虑将sku字段的类型更改为其他类型,例如string,并为带有{{1 }}属性设置为由该字段自动生成的SKU。可以在这里找到相关文档:https://www.sanity.io/docs/slug-type