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

2 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.