How to populate component that comes from a deep nested relation

System Information
  • Strapi Version: 3.4.2
  • Operating System: Windows 10 Pro - 20H2
  • Database: MySql
  • Node Version: 12.19.0
  • NPM Version: 6.14.9
  • Yarn Version: 1.22.5

I have the following code:

find(params, populate) {
return strapi
  .query("menu")
  .find(params, [
    "categories",
    "categories.plates",
    "categories.plates.image",
    "categories.subcategories",
    "categories.subcategories.plates",
    "categories.subcategories.plates.image",
  ]);
},

in which categories, subcategories and plates are all models.

And plates also has a repeatable component called variants:

"variants": {
  "type": "component",
  "repeatable": true,
  "component": "menu.options"
},

I tried putting categories.plates.variants in the find and findOne methods and it didn’t work. Am I missing something? I know there’s a lot of nested stuff in it and I’m open to suggestions.

Thanks

Hi @Rendon

Were you able to find a solution here? I’m also unable to populate some relation fields in a component. Just getting an array of IDs.

Hi Ajisilva :wave:,

I’ve added the following code to the controller of “products”. The code will populate the relation field “category” and the deep nested relation “gender” which is attached to “category”. Should work with Mongoose as a database.

async find(ctx) {
return strapi
  .query("products")
  .find(ctx.query, [
    {
      path: "category",
      populate: {
        path: "gender"
      }
    }
  ]);
}

Good luck!

Thanks @devwearsprada,

I’ve tried something similar to this, but it did not work. In my case, I am trying to populate a relation inside a component that is being used in a dynamic zone.

For reference, here’s my schema and the code I’ve tried:

    await strapi.query("home").model.find().populate({
      path: "content",
      populate: {
        path: "activity_series",
        populate: {
          path: "activities"
        }
      }
    });

The query above will throw this mongoose error:
MissingSchemaError: Schema hasn't been registered for model "home".

As a workaround, I am just making a second query with the IDs that are being returned and it works fine. But, it would be awesome to find a solve to this. Thanks!

Hi, has anybody found a simple solution to this issue that’s not a workaround?

I’m able to do this with this

let activeOrder = await strapi
      .query("order")
      .findOne({ status: "draft", "user.id": user.id }, [
        "order_items",
        "order_items.product",
      ]);

product is related to order_items.

2 Likes

I have the same issue :sob:
Im using MySQL
And I have somethings like this

subCategorias – belongsToMany → preguntas –belongsToMany → respuestas

And respuestas has 3 components fields

I’ve found the following the only sollution that works for me > Mongo, populate nested field only from collection · Issue #9078 · strapi/strapi · GitHub hope that helps!

I found a fix in strapi-connector-bookshelf

In populate.js, juste before the continue;

_.extend(acc, {
          [newKey]: extendWithPopulateQueries(
            [obj[newKey], acc[newKey]],
            queryOptionsToQueryMap(queryOptions, { model: tmpModel })
          ),
        });
        
        continue;
1 Like

If the population is of the same component, choose the deepest level.

All that needs to be done is to populate categories.subcategories.plates.image