简体   繁体   English

没有响应 Strapi v4 的关系字段

[英]No relational fields in response Strapi v4

I have a problem where none of the relational fields are present in the responses after fetching my data.我有一个问题,在获取我的数据后,响应中不存在任何关系字段。 When I look to the schema of one of my schemas with a relation, I see that the relational fields are present in the attributes object.当我查看具有关系的模式之一的模式时,我发现关系字段存在于属性 object 中。 But still I only get the non-relational fields in my response.但我仍然只在我的回复中得到非关系字段。

This is one of my schemas这是我的模式之一

{
  "kind": "collectionType",
  "collectionName": "activities",
  "info": {
    "singularName": "activity",
    "pluralName": "activities",
    "displayName": "activity"
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "name": {
      "type": "string"
    },
    "date": {
      "type": "date"
    },
    "subcategory": {
      "type": "relation",
      "relation": "oneToOne",
      "target": "api::subcategory.subcategory"
    },
    "members": {
      "type": "relation",
      "relation": "manyToMany",
      "target": "api::member.member",
      "inversedBy": "activities"
    }
  }
}

In Strapi v4 relations are not populated when fetching entries by default.Strapi v4中,默认情况下在获取条目时不会填充关系。

Explaination:解释:

Queries can accept a populate parameter to explicitly define which fields to populate, with the following syntax:查询可以接受populate参数来显式定义要填充的字段,语法如下:

GET /api/:pluralApiId?populate=field1,field2

Example request: Get books and populate relations with the author's name and address示例请求:获取书籍并使用作者的姓名和地址填充关系

GET /api/books?populate=author.name,author.address

For convenience, the * wildcard can be used to populate all first-level relations:为方便起见, * 通配符可用于填充所有一级关系:

Example request: Get all books and populate all their first-level relations示例请求:获取所有书籍并填充它们的所有第一级关系

GET /api/books?populate=*

Example request: Get all books and populate with authors and all their relations示例请求:获取所有书籍并填充作者及其所有关系

GET /api/books?populate[author]=*

Note: Only first-level relations are populated with populate=*.注意:只有第一级关系使用populate=*. Use the LHS bracket syntax (ie [populate]=* ) to populate deeper:使用 LHS 括号语法(即[populate]=* )进行更深的填充:

Example request: Get all relations nested inside a "navigation" component in the "global" single type示例请求:获取嵌套在“全局”单一类型中的“导航”组件内的所有关系

GET /api/global?populate[navigation][populate]=*

Solution:解决方案:

Change your API url to one of the following and you should be able to see the related fields populated in the response.将您的 API url 更改为以下之一,您应该能够看到响应中填充的相关字段。

GET /api/activities?populate=subcategory,members

OR或者

GET /api/activities?populate=*

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM