Strapi CMS: Fetch Nested Content

对着背影说爱祢 提交于 2020-08-04 11:40:23

问题


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.

Person: Name, Age

Address: City, Country

Contact: Code, Number

Person has one Address

Address has many Contacts

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?


回答1:


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/documentation/3.0.0-beta.x/concepts/queries.html#api-reference



来源:https://stackoverflow.com/questions/59085678/strapi-cms-fetch-nested-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!