Strapi v4 populate nested field

Hey,

is there a way to populate nested fields with the new query engine of v4?
The docs only show the first level.

In may case I have repeatable component accessories containing a relation to collection accessory (about 30), which means I would start 30 more queryies to fetch them by id.

What I would like to have is something like this:

      data.pdfContent = await strapi.db
        .query("api::pdf-content.pdf-content")
        .findOne({
          where: { id: body.pdfContent.id },
          populate: {
            accessories: { accessory: true },
          },
        })

Is there another way to archive this, or is something planned?

1 Like

Hey,

I’m not sure if you figured this out but I had a similar situation and you are able to nest the populates. So you can try something like:

data.pdfContent = await strapi.db
  .query("api::pdf-content.pdf-content")
  .findOne({
    where: { id: body.pdfContent.id },
    populate: {
      accessories: {
        populate: {
          accessory: true,
        },
      },
    },
  });
1 Like

You are my hero. It works. :slight_smile:

Hello @joshbuilds how can you update nested data? in your case/example how would you update accessories and accessory relationship?

@joshbuilds I have:

const data = await strapi.db
        .query('api::calendar.calendar')
        .findOne({
          where: { id: calendar_id },
          populate: {
            calendar: {
                reservations:true
            },
          },
        })

"calendar is repeatable component with few text, and integer fields, and relationship to many “reservations”
when i run code i get all information except “reservations”

do you maybe know what I’m doing wrong? Thank you in advance

@tw1t611 glad that worked!

@WardenCommander For the nested query I don’t think you need the upper level calendar the way you have described the relationship. I think your query should either be one of the two following based on how your data is nested.

In regards to updating nested data, most of my content we update from the admin UI so it is handled automatically so I’m not sure how to really do it currently. Because it is open source someone can probably dig into the source code to understand how the core function works.

or

Hello @joshbuilds thank you for update, first option is what i was looking for, i need upper level of calendar as each day can have multiple reservations, and i want to know what reservations are on what day.

I’m doing all this so that when i create new entry in “reservations” collection type, it automatically close those days in corresponding calendar

@joshbuilds do you know how to populate nested data in lifehook? "lifecycle.js again its repeatable component.

 beforeUpdate: async (data) => {
    console.log("beforeUpdate Lifecycle hook calendar")
    console.log(data);
  },

hi @WardenCommander
Did you find a solution to have changed nested data in lifecycle hooks?

thanks a lot

@bbordet I had to change concept, and i’m now using collection type instead of component.

this solved some problems but it generated new one.

for me best solution would be that repeatable component has life-cycle hook like collection types

1 Like