0

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

Community
  • 1
  • 1
yokodev
  • 1,266
  • 2
  • 14
  • 28

2 Answers2

2

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));
Daff
  • 43,734
  • 9
  • 106
  • 120
  • Working Perfectly... Thanks. On another case I was having a bad request 400 and nothing more on the console. but on the browser I got a "errors" Object with all the details, I'm not sure if its because I'm using feathers-client... the question is where is this "errors" object coming from cause I need to bubble up/handle the info to the user – yokodev Feb 14 '17 at 17:10
  • Depends on how you set up your logging. With the current default (https://github.com/feathersjs/feathers-chat/blob/master/src/middleware/logger.js#L18) you can also log `error.errors`. – Daff Feb 14 '17 at 18:02
  • Got It !!!, That's exactly what I need. is there a way to throw this back to the client so that I can show a popup or something? – yokodev Feb 14 '17 at 21:03
0

I was trying to use this code, but, it does not work like plug and play.

after some play with the app service I figured out the code below

    async find(params) {
        const { service, path } = params.query;

        const values = await this.app.service(service).Model.attributes[path].values;

        return values || [];
      }

      setup(app) {
        this.app = app;
      }

I am not sure if it is a thing of what database is been used, in my case I am in development environment, so, I am using sqlite.

judicapo
  • 45
  • 4