Search code examples
javascriptnode.jsmongodbfeathersjs

Get model data from monogo database basing on custom ID name in featherJs api


I would like to get user data based on id attribute mentioned in URL : (/user/488/all) using FratherJS framework

   var mongooseService = require('feathers-mongoose');
    ...
    app.use('user/:id/all', mongooseService({
                name: 'agency',
                Model: require('models/user') //user.id is the ID of user model 
            }));
    ...

i don't want to use this url : /user/488


Solution

  • Feathers standard URLs are intentionally built around REST URL best practises so, although not impossible, I would only break with it if there is a very good reason. To be compatible with existing clients you can create aliases using a custom service:

    const mongooseService = require('feathers-mongoose');
    
    app.use('/users', mongooseService({
        name: 'agency',
        Model: require('models/user') //user.id is the ID of user model 
    }));
    
    class UserAliases {
      async find(params) {
        const { id } = params.route;
    
        return this.app.service('users').get(id, params);
      }
    
      setup(app) {
        this.app = app;
      }
    }
    app.use('/user/:id/all', new UserAliases());