Search code examples
feathersjs

FeathersJS: Call service with express route parameters


I have a service defined like this:

app.use(`/teams/:${id}/members`, teamsMembersService);

Now I would like to call this service from another service. According to the documentation I can access different services using the app object. Like so:

let result = await app.service('teams').find();

That works well. Unfortunately this does not:

let result = await app.service('teams/1/members').find();

Because the service is undefined.

How can I call this service with route parameters?


Solution

  • await app.service('teams/1/members').find(); will work if you are using the REST client but not otherwise. On the server you can pass id in params:

    app.service(`/teams/:${id}/members`).find({ id })
    

    For use via Socket.io (and generally a good idea) is to add the parameter to params.query:

    app.service(`/teams/:${team_id}/members`).hooks({
      before: {
        find(hook) {
          if(hook.params.team_id) {
            hook.params.query.team_id = hook.params.team_id;
          }
        }
      }
    })
    

    Now you can do

    app.service(`/teams/:${id}/members`).find({ query: { team_id: id } })