我正在使用 Strapi CMS 并努力获取嵌套/深层内容的数据。例如:假设我创建了以下内容类型并定义了关系。
人:姓名、年龄
地址:城市,国家
联系方式:代码、号码
一个人有一个地址
地址有很多联系人
现在的问题是,当我访问 '/persons' 时,我只得到 Name、Age 和 Address 对象。但是地址对象没有与地址关联的联系信息。
有人可以帮我解决这个问题或给我指出任何这样的文章吗?
Rol*_*ogh 15
首先,您需要一个自定义控制器功能。在/api/person/controllers/Person.js可以导出自定义查找功能。在那里您可以定义要填充的字段:
module.exports = {
find: ctx => {
return strapi.query('person').find(ctx.query, ['address', 'contact']);
},
};
Run Code Online (Sandbox Code Playgroud)
另一个解决方案也适用于我:
module.exports = {
find: ctx => {
return strapi.query('person').find(ctx.query, [
{ path: 'address' },
{ path: 'contact' },
]);
},
};
Run Code Online (Sandbox Code Playgroud)
编辑了更深一层的示例:
module.exports = {
find: ctx => {
return strapi.query('person').find(ctx.query, [
{
path: 'address',
populate: {
path: 'contacts',
},
},
]);
},
};
Run Code Online (Sandbox Code Playgroud)
作为参考,请参阅最新的测试版文档:
https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#api-reference
| 归档时间: |
|
| 查看次数: |
8308 次 |
| 最近记录: |