Reputation: 11
I've created a before hook in my users service that's meant to include a record from the organizations model. The relationship between users and organizations is
users.belongsToMany(models.organizations,{through: 'users_to_organizations'});
The before hook is called attach-orgnaization.js and looks like
module.exports = (options = {}) => {
return async context => {
const organizations = context.app.services.organizations.Model;
context.params.sequelize = {
include: [{model: organizations}]
};
return context;
};
};
It's set in users.hooks.js like this
....
const attachOrganization = require('../../hooks/attach-organization');
module.exports = {
before: {
all: [],
find: [ authenticate('jwt') ],
get: [authenticate('jwt'), attachOrganization()],
....
When I GET /users?id=1
I get the user back with nothing about the org that the user is associated with in users_to_organizations
, which happens to be org 3 "test org".
I was expecting to see additional organization related fields in the response.
Instead I see only
{
"total": 1,
"limit": 10,
"skip": 0,
"data": [
{
"id": 1,
"email": "[email protected]",
"googleId": null,
"githubId": null,
"createdAt": "2020-04-29T04:26:49.541Z",
"updatedAt": "2020-04-29T04:26:49.541Z"
}
]
}
Stepping through the debugger (pycharm) I can see that the code in the hook is executing.
I guess that either sequelize isn't seeing the relationship or feathers is not adding the include to the query.
What might I be missing?
Thanks!
Upvotes: 0
Views: 469
Reputation: 1979
In feathersjs, Get Method has 2 types
Get
: To get a specific data . Ex: GET /user/:id
(id is the user id)Find
: To get all data with filters , Ex : GET /user?name=xyz
(It'll return all the documents with name xyz)You have attached the attachOrganization()
in the get method and calling /users?id=1
which is FIND method.So either call it by /users/1
id 1 is your _id
or add the attachOrganization()
to the hooks of find
method too
Upvotes: 0