Skip to content
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

fix(has many): fix link of has many #944

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/serializers/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,20 @@ function ResourceSerializer(
formatFields(records);
}

const setHasManyToEmptyArray = (record) => {
schema.fields.forEach((field) => {
if (!record[field.field] && _.isArray(field.type) && field.reference) {
record[field.field] = [];
}
});
Comment on lines +162 to +166
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done in the SmartFieldsValuesInjector but the thing is it has been changed to do it only for requested field (it was a side effect but it makes the has many work 😅 )

};

return new P((resolve) => {
if (_.isArray(records)) {
let smartFieldsValuesInjector;
resolve(P
.map(records, (record) => {
setHasManyToEmptyArray(record);
smartFieldsValuesInjector = new SmartFieldsValuesInjector(
record,
modelName,
Expand All @@ -178,6 +187,7 @@ function ResourceSerializer(
return result;
}));
} else {
setHasManyToEmptyArray(records);
resolve(new SmartFieldsValuesInjector(records, modelName, fieldsPerModel).perform());
}
})
Expand Down