Strapi CMS:获取嵌套内容 [英] Strapi CMS: Fetch Nested Content

查看:37
本文介绍了Strapi CMS:获取嵌套内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Strapi CMS 并且正在努力获取嵌套/深层内容的数据.例如:假设我创建了以下内容类型并定义了关系.

I am using Strapi CMS and struggling with fetching the nested/deep content’s data. E.g.: Let’s say, I have below content types created and relations are defined.

人物:姓名、年龄

地址:城市、国家

联系人:代码、号码

一个人有一个地址

地址有很多联系人

现在的问题是,当我访问 ‘/persons’ 时,我只得到 Name、Age 和 Address 对象.但是地址对象没有与地址关联的联系信息.

Now the problem is, when I access ‘/persons’, I get only Name, Age and Address object. But address object does not have the contact information associated with the address.

有人可以帮我解决这个问题,或者给我指出任何此类文章吗?

Can somebody help me to get this resolved or point me towards any such article?

推荐答案

首先,您需要一个自定义控制器功能.在 /api/person/controllers/Person.js 中,您可以导出自定义查找功能.您可以在那里定义要填充的字段:

Firstly you'll need a custom controller function for this. In /api/person/controllers/Person.js you can export your custom find function. There you can define which fields you want to populate:

module.exports = {
  find: ctx => {
    return strapi.query('person').find(ctx.query, ['address', 'contact']);
  },
};

另一种解决方案也适用于我:

Another solution works for me as well:

module.exports = {
  find: ctx => {
    return strapi.query('person').find(ctx.query, [
       { path: 'address' },
       { path: 'contact' },
    ]);
  },
};

使用更深一层填充的编辑示例:

Edited example with one level deeper populate:

module.exports = {
  find: ctx => {
    return strapi.query('person').find(ctx.query, [
      {
        path: 'address',
        populate: {
          path: 'contacts',
        },
      },
    ]);
  },
};

有关参考,请参阅最新的测试版文档:

For reference see the most recent beta docs:

https://strapi.io/文档/3.0.0-beta.x/concepts/queries.html#api-reference

这篇关于Strapi CMS:获取嵌套内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆