Search code examples
node.jsfile-uploadstrapi

upload a file to an array of component in an entity in strapi


I have a collection in that there is an array of component product_licenses which take data along with image. if I upload an image to the Lis tag it works. but when I try to upload to the first element of the array using the license tag it fails since it is inside.

const entity = await strapi.services[
            'product-registration'
          ].create(input);

          if (lis) {
            // automatically uploads the files based on the entry and the model

            // eslint-disable-next-line max-len
            await strapi.plugins.upload.services.upload.uploadToEntity({
              id: entity.id || entity._id,
              model: 'product-registration',
              field: 'lis'
            }, lis);

            await strapi.plugins.upload.services.upload.uploadToEntity({
              id: entity.id || entity._id,
              model: 'product-registration',
              field: 'product_licenses[0].license'
            }, lis);
          }

Is there any way I can achive this?


Solution

  • In order to link the uploaded image to a component field, you should set the params argument as follows:

    • id: The product_license component id
    • model: Here you have to concat the component’s category name + the component name (category_name.product_license)
    • field: your component image field name