How to push new data in repeatable component?

System Information
  • Strapi Version: 3.3.3
  • Database: Mongodb

Hello! I have a documents field in my user model, wich is a repeatable components with some fields (string, number, and file).

When i want to add a document to this user, i have to fetch this user to get all his documents, map them to give to Strapi the expected format, and then update him with a new array of all mapped documents and the new one.

I’m in a plugin controller.

      const user = await strapi.plugins[userPluginName].services.user.fetch({ id: idToUpdate });
      const documents = user.documents.map((doc) => mapDocument(doc));
      await strapi.plugins[userPluginName].services.user.edit(
        {
          id: idToUpdate,
        },
        { documents: [...documents, document] }
      );

Is there a simple way to add a new document to this user without having to fetch him and spread its existing documents?

Thanks!

Had the same question, theres no way around it. You must map the existing data.

by the way you are doing it, the ids of the documents will change evry update right?

Yes as @Fernando_Moreno mentioned, you have to map them all and send them with the new one. Omitting one is how you delete them.

The documentation probably doesn’t explain the limitation very well. Maybe a bit of an update would avoid common question.

I am putting together an endpoint that is a POST /hubs/:id with events (my repeatable component) in the body to then append it server side. Looking on best way to achieve this - raw SQL maybe the answer.

1 Like

And voila – here it is

This will be much easier in the v4 as there will actually be functions on the entity service and we are adding an “actions” system so you can create your own custom actions.

1 Like

Can i update to v4 if i am using mongodb?

No, with v4 we are dropping native support for MongoDB

@DMehaffy So, how do you do this in v4? :grinning: Adding an item to a repeatable list with the entity service I mean

3 Likes

@DMehaffy Can you please share some details on the v4 implementation of adding an entry to the repeatable component instead of replacing all? I especially curious if there is some way to do it though api

More or less the same as in v3, you need all the existing ones (with their IDs) and you add another compo object with no id field and it will create a new one.

You have to create in database repeatable components. And update your api collection data.

    let option1 = await strapi.db.query('product.option').create({
        data: {
            value: "XXL"
        }
    })

    let option2 = await strapi.db.query('product.option').create({
        data: {
            value: "XL"
        }
    })

    let variant1 = await strapi.db.query('product.variants').create({
        data: {
            options: [option1, option2],
            name: "size"
        }
    })

    let product = await strapi.db.query('api::product.product').update({
        where :{
            slug: "qraxiss-x"
        },
        populate: {
            variants: "*"
        },
        data : {
            variants: [variant1]
        }
    })

Here is the result! have a nice day!