Feathers: Nested Routes

Created on 13 Sep 2017  路  6Comments  路  Source: feathersjs/feathers

Hi,
I have 2 services, Users and Addresses. I tried doing the Nested route example as in:
https://docs.feathersjs.com/faq/readme.html#how-do-i-do-nested-or-custom-routes

this results in:
/users/:userId/addresses

When a Users has only 1 address, it works. When the user has multiple addresses, I receive an error:
localhost didn鈥檛 send any data.
ERR_EMPTY_RESPONSE

However, if I do:
/addresses/?userID=xxxx
It works as expected for both single and multiple addresses.

All services are using sequelize/mysql.
I'm using:
"body-parser": "^1.17.2",
"compression": "^1.7.0",
"cors": "^2.8.4",
"feathers": "^2.1.7",
"feathers-authentication": "^1.2.7",
"feathers-authentication-hooks": "^0.1.4",
"feathers-authentication-jwt": "^0.3.2",
"feathers-authentication-local": "^0.4.3",
"feathers-configuration": "^0.4.1",
"feathers-errors": "^2.9.1",
"feathers-hooks": "^2.0.2",
"feathers-hooks-common": "^3.6.1",
"feathers-rest": "^1.8.0",
"feathers-sequelize": "^2.2.0",
"feathers-socketio": "^2.0.0",
"feathers-swagger": "^0.5.0",
"helmet": "^3.8.1",
"mysql2": "^1.4.0",
"sequelize": "^4.4.2",
"serve-favicon": "^2.4.3",
"skypager-service": "^5.4.0",
"winston": "^2.3.1"

Most helpful comment

Does it work if you do something like:

app.use('/users/:userId/addresses', {
  find(params) {
    return this.app.service('addresses').find({
      query: Object.assign({
        userId: params.userId
      }, params.query);
    });
  },

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

?

All 6 comments

A little bit more information. I tried to setup exactly the same structure, for a different service, and it turns out to fail irrespective of the expected result (arrays or single items).
Then I set the DEBUG flag and I got this output any time I hit the nested routes:

  • feathers:rest REST handler calling find from /users/123/addresses
  • info: after: users/:userId/addresses - Method: find
  • feathers-errors NotFound(404): Page not found
  • feathers-errors {}
  • Error: Can't set headers after they are sent.

Even when I call /users/123/addresses and it does return a single addrress, the log still shows the same header error.

Any ideas on what is possibly wrong with the nested routing?

Thank you in advance.

Does it work if you do something like:

app.use('/users/:userId/addresses', {
  find(params) {
    return this.app.service('addresses').find({
      query: Object.assign({
        userId: params.userId
      }, params.query);
    });
  },

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

?

Hi,
It works as expected when using the code above, except for an extra ";" after "params.query)". Is there something wrong on the code provided on the FAQ, or on my overall setup?

I don't think it's your setup. I'm pretty sure it's related to https://github.com/feathersjs/feathers/issues/566 which will be fixed in the next version but causes some strange issues like yours at the moment.

Ok, so for the time being, the correct solution is to implement nested routes as you pointed me out.

I tried it on a different service, and it worked as well.

My only question would be related to the other methods (create, patch,update), would I just implement like you did for the find?

Thank you for your help.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codeus-de picture codeus-de  路  4Comments

rstegg picture rstegg  路  3Comments

jordanbtucker picture jordanbtucker  路  4Comments

NetOperatorWibby picture NetOperatorWibby  路  4Comments

andysay picture andysay  路  3Comments