Simple nested array issue

System Information
  • Strapi Version: 3.3.0
  • Operating System: Mac
  • Database: sql-lite(quick start)
  • Node Version: 14.15.0
  • NPM Version: 6.14.8
  • Yarn Version: 1.22.4

Hello,

This is my first time using Strapi. My current response for localhost:1337/defaults is Screen Shot 2020-11-09 at 2.24.02 PM

This allows me to have multiple Sections. These sections should have an array of Content but for some reason, relationships nested this far won’t show. Any help is appreciated. let me know if you need anything else from me!

Can you give the structure of your content-type models? (you can grab the model settings from ./api/*/models/*.settings.json)

{
  "kind": "collectionType",
  "collectionName": "defaults",
  "info": {
    "name": "default",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": true,
    "draftAndPublish": true
  },
  "attributes": {
    "showProfile": {
      "type": "boolean",
      "default": true,
      "required": true,
      "unique": true
    },
    "sections": {
      "via": "defaults",
      "collection": "section",
      "dominant": true
    }
  }
}




{
  "kind": "collectionType",
  "collectionName": "sections",
  "info": {
    "name": "section",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": true,
    "draftAndPublish": true
  },
  "attributes": {
    "title": {
      "type": "string"
    },
    "contents": {
      "collection": "content",
      "via": "sections",
      "dominant": true
    },
    "defaults": {
      "collection": "default",
      "via": "sections"
    }
  }
}



{
  "kind": "collectionType",
  "collectionName": "contents",
  "info": {
    "name": "content",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": true,
    "draftAndPublish": true
  },
  "attributes": {
    "value": {
      "type": "string",
      "required": true,
      "unique": true
    },
    "activities": {
      "collection": "activity",
      "via": "contents",
      "dominant": true
    },
    "sections": {
      "via": "contents",
      "collection": "section"
    }
  }
}

So you are trying to populate defaults => sections => contents right?

By default Strapi only goes one level deep (so defaults => sections) but you can customize the population. Replace the default find service with the example from our documentation here: https://strapi.io/documentation/v3.x/concepts/services.html#find

And we need to customize the population array:

Path: ./api/default/services/default.js

module.exports = {
  /**
   * Promise to fetch all records
   *
   * @return {Promise}
   */
  find(params, populate) {
    return strapi.query('default').find(params, ["section", "section.content"]);
  },
};
3 Likes

Thank you so much!

1 Like

Note you will need to do the same for findOne if you need it

Would you be able to provide an example with a component?
I have a single type “home” that contains one DZ with a component that has relationship to news
All good. But each news has a component within. I have not being able to access to the component of news “Item” field in the example below.

	{
  home {
    Home {
      __typename
      ... on ComponentNewsNewsItems {
        news_items {
          id,
          QuickTitle,
          Item {
            Title
          }
        }
      }
    }
  }
}
//returns
{
  "data": {
    "home": {
      "Home": [
        {
          "__typename": "ComponentNewsNewsItems",
          "news_items": [
            {
              "id": "1",
              "QuickTitle": "QuickNews",
              "Item": null
            }
          ]
        }
      ]
    }
  }

And

{
  newsItems {
    id,
    QuickTitle,
    Item {
      Title
    }
  }
}
//returns
{
  "data": {
    "newsItems": [
      {
        "id": "1",
        "QuickTitle": "QuickNews",
        "Item": {
          "Title": "Title"
        }
      }
    ]
  }
}

I cannot apply params / where clause on section.content fields. Is there any work around for that?

Hi, @DMehaffy thanks for providing a solution, unfortunately this doesn’t seem to work for me. My structure is as follows:

segment > slide > map > map_locations

where segment is a top level collection type, slide is a component and map is a child collection type. Map has repeated components called map_locations but these are not showing up.

Any help would be appreciated.

Component population has known issues on the v3, right now we don’t have any recommendations but we are fixing this for the v4

Hi , I got a similar problem here:

but your solution didn’t work in my case. The link that you provided is also no longer available.

has this been resolved in v4 @DMehaffy? i’m currently on 4.0.0 and still have no luck finding component data when i fetch from /api/home which has a single component in it (no other fields) with 5 fields w/i it.

thanks!

We don’t populate components by default in v4 but you can now populate stuff at request time as deep as you want (within reason of course):

thanks, @DMehaffy , but my issue had been more around GraphQL (via Gatsby nodes created from a Strapi source) being able to identify component data within a given Single Type (e.g. Home)

does that make sense?