How to get the attributes type in the API response?

System Information
  • Strapi Version: 3.14.0
  • Operating System: OSX
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

I have an Enum field city = ‘Paris’ | ‘Lyon’ | ‘Marseille’

I would like to be able to receive all the 3 cities in the api response in my front-end to display a select with these options.

it’s declared in models/*.settings.json → “type”: “enumeration”,
“enum”: [“Paris”, “Lyon”, “Marseille”]

thanks

We don’t currently have a method to return these options to the user via REST/GraphQL. You could create a custom controller to handle this though by loading in the model.

Something like:

Path: ./api/test/controllers/test.js

module.exports = {
  async getTypes(ctx) {
    const types = await strapi.models['test'].attributes
    return types;
  },
};

Path: ./api/test/config/routes.json
(Just make sure this route is above the .findOne route or it will fail)

{
  "routes": [
...
        {
      "method": "GET",
      "path": "/tests/types",
      "handler": "test.getTypes",
      "config": {
        "policies": []
      }
    },
...
  ]
}
1 Like