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

Potential mistake prevents model data modification in pre-save hooks #109

Open
FlyingDR opened this issue Oct 8, 2021 · 1 comment
Open

Comments

@FlyingDR
Copy link

FlyingDR commented Oct 8, 2021

Please consider this piece of model.js source:

warehouse/lib/model.js

Lines 161 to 164 in 067dd53

return execHooks(schema, 'pre', 'save', data).then(data => {
// Insert data
this.data[id] = result;
this.length++;

It can be seen, that pre save hook is received data variable and receives (potentially modified) data in then() (at the line 161), but ignores this information and inserts previously defined result variable instead at the line 163.

It effectively results into lack of ability to perform any data modifications in pre save hooks.

Same approach is used for updating and replacement of the model data.

Since logic of this piece of code remains untouched from the very beginning - I'm not sure if it is intentional (but rather confusing) decision or a bug that hides for a long time.

@stevenjoezhang
Copy link
Member

I have searched the source code of Hexo, there are several pre-save or pre-remove hooks, but none of them modify the data. I'd rather believe that such behavior is intentional

To make it less confusing, maybe we could Object.freeze the data before exec Hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants