Is not a function ERROR - this.isModified is not a function
See original GitHub issueWhen I use following code:
userSchema.pre('save', (done) => {
if(this.isModified('password')) {
bcrypt.hash(this.password, null, null, (err, hash) => {
if(err) return next(err);
this.password = hash;
this.updated_at = new Date().toISOString();
done();
});
} else {
return done();
}
});
I receive following Error:
TypeError: this.isModified is not a function
at model.userSchema.pre (C:\code\project\core\models\user.js:26:11)
at _next (C:\code\project\node_modules\hooks-fixed\hooks.js:62:30)
at fnWrapper (C:\code\project\node_modules\hooks-fixed\hooks.js:186:8)
at model.Object.defineProperty.value.fn (C:\code\project\node_modules\mongoose\lib\schema.js:221:11)
at _next (C:\code\project\node_modules\hooks-fixed\hooks.js:62:30)
at fnWrapper (C:\code\project\node_modules\hooks-fixed\hooks.js:186:8)
at C:\code\project\node_modules\mongoose\lib\schema.js:196:17
at C:\code\project\node_modules\kareem\index.js:127:16
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
Problem:
this
is not a Document, Query or Schema. It’s value is{}
How can I solve that?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:13
- Comments:11
Top Results From Across the Web
user.isModified is not a function when doing a pre update in ...
You get an error because the arrow function changes the scope of 'this.' Just use UserSchema.pre('save', function(next){}).
Read more >[Solved]-TypeError: this.isModified is not a function-node.js
Coding example for the question TypeError: this.isModified is not a function-node.js.
Read more >How to Handle JavaScript Uncaught TypeError: “x” is Not a ...
The Javascript error TypeError: "x" is not a function occurs when there is an attempt to call a function on a value or...
Read more >mongoose.isModified JavaScript and Node.js code examples
Best JavaScript code snippets using mongoose.isModified(Showing top 15 results out of 414) · Most used mongoose functions · Popular in JavaScript.
Read more >What exactly does .isModified method do in .pre hook ... - Reddit
save(); res.send("done"); });. I'm also not calling 'next' function from within my .pre hook so the request should ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You are using an arrow operator for the callback, which changes the scope of
this
. If you define a regular callback you should be fine, e.g.:Thanks, works for me too.