Search code examples
reactjsexpressredux-formredux-sagafeathersjs

FeathersJS custom API method that retrieves enum Type, to fill a Dropdown in React


So I'm trying to fill a select component with a enum type from mongoose

In my user service the schema looks something like :

firstName: { type:String, required: true },
   ...
ris:{type: String, default: 'R', enum:['R', 'I', 'S']},

In my feathers service I can access the Model with "this.Model"

so in any hook I can do:

this.Model.schema.path('ris').enumValues); //['R','C','I']

and I get the values from the enum type.

Now since I can't create custom API methods other that the officials ones

How can I create a service method/call/something so that I can call it in my

componentDidMount(){ var optns= this.props.getMyEnumsFromFeathers}

and have the enum ['R','C','I'] to setup my dropdown

I'm Using React/Redux/ReduxSaga-FeathersJS


Solution

  • I'd create a service for listing Enums in the find method:

    class EnumService {
      find(params) {
        const { service, path } = params.query;
        const values = this.app.service(service).Model.schema.path(path).enumValues;
    
        return Promise.resolve(values);
      }
    
      setup(app) {
        this.app = app;
      }
    }
    
    app.use('/enums', new EnumService())
    

    Then on the client you can do

    app.service('enums').find({ query: {
        service: 'myservice',
        path: 'ris'
      }
    }).then(value => console.log('Got ', values));