New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getAll request on '_MANY' association #191
Comments
Hi @Clemssss, thanks for reaching out. I believe I understand the issue you describe, though I will need some more information since I am unable to replicate it on my end.
Let's start from there and see if we can figure out what's going wrong. Thanks! |
Thank @JKHeadley for your speedy response !
{
"_id": "5d5db71b1c99220acc90ae45",
"companies": [
{
"_id": "5d5cae373356752c4f2d4416",
"subsidiaries": [],
"name": "string",
"naf": "string",
"siret": "string",
"createdBy": "5d5c9e1a6b233e246ad4f611",
"createdAt": "2019-08-21T02:36:39.450Z",
"institutions": []
}
],
"email": "test@test.com",
"password": "$2b$10$N4QlJTSrMfurtS3/U13bi.Dr0e2CxidEufFUrKT.PuEgY/DLFdaWW",
"firstName": "string",
"lastName": "string",
"rights": {
"user": true,
"oppusUser": false
},
"createdAt": "2019-08-21T21:26:51.661Z"
} Once again thanks very much for your time and your reply! |
Hmm, yes |
Sure. const UserSchema = new Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
phone: { type: String },
email: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true },
authyId: { type: String },
companies: [{ type: Schema.Types.ObjectId, ref: 'company' }],
rights: { type: Schema.Types.Mixed, default: {} },
savedData: { type: Schema.Types.Mixed, default: {} },
});
module.exports = () => {
UserSchema.statics = {
collectionName: 'user',
routeOptions: {
associations: {
companies: {
type: '_MANY',
alias: 'companies',
model: 'company',
},
},
find: {
pre: (_id, query, request) => {
const user = request.auth.credentials;
if (!_id || user._id !== _id) {
throw Boom.unauthorized('You are not authorized with this account.');
}
return {
$embed: ['companies'],
};
},
},
list: {
pre: (query, request) => {
const user = request.auth.credentials;
if (!user.rights.oppusUser) {
throw Boom.unauthorized('You are not authorized with this account.');
}
return {
$embed: ['companies'],
};
},
},
},
};
return UserSchema;
};
const CompanySchema = new Schema({
name: { type: String, required: true },
naf: { type: String, required: true },
siret: { type: String, required: true },
institutions: [{
name: { type: String },
address: {
city: { type: String },
street: { type: String },
complementary: { type: String },
zipCode: { type: String },
},
numberOfEmployees: { type: Number },
}],
subsidiaries: [{ type: Schema.Types.ObjectId, ref: 'company' }],
});
module.exports = () => {
CompanySchema.statics = {
collectionName: 'company',
routeOptions: {
associations: {
subsidiaries: {
type: '_MANY',
alias: 'subsidiaries',
model: 'company',
},
},
find: {
pre: () => ({
$embed: ['subsidiaries'],
}),
},
list: {
pre: () => ({
$embed: ['subsidiaries'],
}),
},
},
};
return CompanySchema;
}; |
Ohh, i find, its the find and list pre-middlewares in the |
Hi @Clemssss, it's because you are overriding the Try changing your middleware to: find: {
pre: (_id, query, request, Log) => ({
...query,
$embed: ['subsidiaries']
}),
},
list: {
pre: (query, request, Log) => ({
...query,
$embed: ['subsidiaries']
}),
}, |
Exactly, im stupid ... Thanks for your response ! |
Not at all, its easy to miss! |
Describe the bug
I want to get '_MANY' association and rest-hapi return all datas of the model of association
To Reproduce
Steps to reproduce the behavior:
Expected behavior
getAll request on '_MANY' association should return only the id's of the association and not fulld datas of the corresponding model.
Desktop (please complete the following information):
Additional context
Sorry for my deplorable English, i hope its comprehensive ! Thanks for your answer.
The text was updated successfully, but these errors were encountered: