简体   繁体   English

Strapi v4:填充时没有关系字段

[英]Strapi v4: no relational fields when populating

I'm trying to populate a specific relation, using the relation name ( categories ) in combination with the populate parameter but it doesn't populate the categories .我正在尝试使用关系名称( categories )与 populate 参数组合来填充特定的关系,但它不会填充categories

When I look at my schema, I see that the relational field is present in the attributes object.当我查看我的模式时,我发现关系字段存在于属性 object 中。 But I still only get the non-relational fields in my response.但我的回复中仍然只得到非关系字段。

I tried every combination mentioned on the Strapi documentation but none of them worked.我尝试了Strapi 文档中提到的所有组合,但都没有奏效。

The find permission is also enabled for the content-types that are being populated which in this case is categories .还为正在填充的内容类型启用了find权限,在本例中为categories

/api/products?populate=*
/api/products?populate[0]=categories
/api/products?populate[categories]=*

My Product schema我的产品架构

{
  "kind": "collectionType",
  "collectionName": "products",
  "info": {
    "singularName": "product",
    "pluralName": "products",
    "displayName": "Product",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "title": {
      "type": "string"
    },
    "images": {
      "type": "media",
      "multiple": true,
      "required": false,
      "allowedTypes": [
        "images"
      ]
    },
    "categories": {
      "type": "relation",
      "relation": "oneToMany",
      "target": "api::category.category"
    }
  }
}

System系统

  • Strapi version: 4.1.8 Strapi 版本: 4.1.8
  • NPM version: 8.3.2 NPM 版本: 8.3.2
  • Node.js version: 16.13.2 Node.js 版本: 16.13.2
  • Database: MySQL数据库:MySQL

you will have to populate inside your controller, as in Strapi documentation您必须在 controller 中填充,如Strapi 文档中所示

Query Engine API: Populating查询引擎 API:填充

strapi.db.query('api::article.article').findMany({
 populate: true,
});

Entity Service API: Populating实体服务 API:填充

const entries = await strapi.entityService.findMany('api::article.article', {
  populate: '*',
});

REST API: Population & Field Selection REST API:人口和领域选择

const qs = require('qs');
const query = qs.stringify({
  fields: ['title', 'body'],
}, {
  encodeValuesOnly: true,
});

If you want to populate everything just use populate: '*'如果您想填充所有内容,只需使用 populate: '*'

If you want to populate a relation or more use populate: [relationOne, relationTwo]如果要填充一个或多个关系,请使用 populate: [relationOne, relationTwo]

You need to enable find permission on the product as well.您还需要启用产品查找权限。 You have to set the find permission on all the relationships (or sub-tables).您必须对所有关系(或子表)设置查找权限。

Use the "role" option.使用“角色”选项。

As an Example If you want to fetch data from db based on some conditions and populate one relationship举个例子如果你想根据某些条件从数据库中获取数据并填充一个关系

const transactions = await strapi
        .query("api::transaction.transaction")
        .findMany({
          where: { user: userId },
          populate: { offer: true },
        });

here the result这里的结果在此处输入图像描述 If you want to populate all relations如果要填充所有关系

const transactions = await strapi
        .query("api::transaction.transaction")
        .findMany({
          where: { user: userId },
          populate: "*",
        });

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

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