question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is not a function ERROR - this.isModified is not a function

See original GitHub issue

When 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:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:11

github_iconTop GitHub Comments

68reactions
VictorGerritsenQLVRcommented, Sep 20, 2016

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.:

userSchema.pre('save',  function(done)  {
});
0reactions
m00zicommented, Apr 26, 2018

Thanks, works for me too.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found