Create entry with repeatable component inside

System Information
  • Strapi Version: 4.1.9
  • Operating System: Fedora Linux
  • Database: mysql
  • Node Version: 16.13.2
  • NPM Version: 8.1.2
  • Yarn Version: N/A

Hi everyone,

I am trying to run a create like this, but the options are not saved:

const createdQ = await strapi.db.query('api::question.question').create({
  data: {
    "id": 3,
    "name": "Q 1.2.1",
    "required": true,
    "createdAt": "2022-05-04T08:41:27.948Z",
    "updatedAt": "2022-05-04T09:13:35.067Z",
    "options": [
      {
        "id": 17,
        "value": "test value",
        "text_type": null
      }
    ]
  }
});
Question Schema looks like this...
{
  "kind": "collectionType",
  "collectionName": "questions",
  "info": {
    "singularName": "question",
    "pluralName": "questions",
    "displayName": "Question",
    "description": ""
  },
  "options": {
    "draftAndPublish": false
  },
  "pluginOptions": {},
  "attributes": {
    "name": {
      "type": "string",
      "required": true
    },
    "required": {
      "type": "boolean",
      "required": true
    },
    "options": {
      "type": "component",
      "repeatable": true,
      "component": "cc-building-blocks.option"
    }
  }
}
Options Component Schema looks like this...
{
  "collectionName": "components_cc_building_blocks_options",
  "info": {
    "displayName": "option",
    "icon": "align-center",
    "description": ""
  },
  "options": {},
  "attributes": {
    "value": {
      "type": "string",
      "required": true
    },
    "text_type": {
      "type": "enumeration",
      "enum": [
        "info",
        "warning"
      ]
    }
  }
}

Any ideas what’s wrong?
I read the docs here and it should work, right?

Cheers!

1 Like

Found the issue and the solution.
The problem was that I was using strapi.db.query and that does not seem to insert nested component objects.
The solution is to use strapi.entityService.create... :

const createdQ = await strapi.entityService.create('api::question.question', {
    data: {
    "id": 3,
    "name": "Q 1.2.1",
    "required": true,
    "createdAt": "2022-05-04T08:41:27.948Z",
    "updatedAt": "2022-05-04T09:13:35.067Z",
    "options": [
      {
        "id": 17,
        "value": "test value",
        "text_type": null
      }
    ]
  }
});
1 Like