admin管理员组文章数量:1287840
I'm trying to populate a specific relation, using the relation name (categories
) in bination with the populate parameter but it doesn't populate the categories
.
When I look at my schema, I see that the relational field is present in the attributes object. But I still only get the non-relational fields in my response.
I tried every bination mentioned on the Strapi documentation but none of them worked.
The find
permission is also enabled for the content-types that are being populated which in this case is 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
- NPM version:
8.3.2
- Node.js version:
16.13.2
- Database: MySQL
I'm trying to populate a specific relation, using the relation name (categories
) in bination with the populate parameter but it doesn't populate the categories
.
When I look at my schema, I see that the relational field is present in the attributes object. But I still only get the non-relational fields in my response.
I tried every bination mentioned on the Strapi documentation but none of them worked.
The find
permission is also enabled for the content-types that are being populated which in this case is 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
- NPM version:
8.3.2
- Node.js version:
16.13.2
- Database: MySQL
3 Answers
Reset to default 6You 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.
Edit: Other choice is to use populate deep from strapi marketplace, just install, configure the default depth and then on every request where you want deep just add populate: deep
you will have to populate inside your controller, as in Strapi documentation
Query Engine API: Populating
strapi.db.query('api::article.article').findMany({
populate: true,
});
Entity Service API: Populating
const entries = await strapi.entityService.findMany('api::article.article', {
populate: '*',
});
REST API: Population & Field Selection
const qs = require('qs');
const query = qs.stringify({
fields: ['title', 'body'],
}, {
encodeValuesOnly: true,
});
If you want to populate everything just use populate: '*'
If you want to populate a relation or more use populate: [relationOne, relationTwo]
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: "*",
});
本文标签: javascriptStrapi v4 no relational fields when populatingStack Overflow
版权声明:本文标题:javascript - Strapi v4: no relational fields when populating - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741325320a2372441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论