javascript - 内容搜索字段值也有一个字段

标签 javascript contentful

例如,我在内容丰富的后端列出了多部电影,我只想查询返回恐怖电影,所以我按类别过滤。

0:fields{
    name: "Some horror movie"
    category: {sys: {…}, fields: {…}} // fields contains in this example: {name: "horror"}
}

请注意,category 本身就是一种内容类型,它有一个 fields 属性,其中只有一个 name 属性。 所以我想我可以通过执行以下操作来过滤它:

client.getEntries({
      'content_type': 'movie',
      'fields.category.fields.name[match]': 'horror'
    })

虽然这会返回 400 错误,但我真的不知道为什么或如何解决这个问题。

最佳答案

查询是正确的,但在过滤字段值时还需要包含对引用内容类型的过滤。您可以在此处阅读更多相关信息:https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references

如果您将此添加到您的查询中,它应该可以工作。

client.getEntries({
  'content_type': 'movie',
  'fields.category.fields.name[match]': 'horror',
  'fields.category.sys.contentType.sys.id': 'horror-content-type-id'
})

您可以在此处找到完整的片段示例:https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references/search-on-references/console/js

请记住将 'horror-content-type-id' 替换为恐怖内容类型的实际 ID。

关于javascript - 内容搜索字段值也有一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52614261/

相关文章:

javascript - Node.JS/ express /蒙戈 : TypeError: Cannot read property 'location' of undefined

graphql - 对 Gatsby 中的多个查询对 GraphQL 查询进行排序

javascript - 使用引用字段更新 contentful 中的条目

javascript - 类型错误 : Converting circular structure to JSON - find error in json

javascript - 如何在静态网站中重用 html/css/javascript?

javascript - TinyMCE - 防止自动聚焦于 mceAddControl 命令

javascript - 如何通过 JavaScript 动态增加输入控制......?

javascript - Contentful.com API 订单/排序查询

graphql - 您如何处理多种类型的数组(例如 : different content blocks) in GraphQL?

javascript - 如何在 node.js 中安全地运行用户提交的模块?