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

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

Closed
ghost opened this issue Sep 18, 2016 · 11 comments
Closed

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

ghost opened this issue Sep 18, 2016 · 11 comments

Comments

@ghost
Copy link

ghost commented Sep 18, 2016

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?

@VictorGerritsenQLVR
Copy link

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)  {
});

@ghost
Copy link
Author

ghost commented Sep 20, 2016

Ok. Thanks. It works.

@ghost ghost closed this as completed Sep 20, 2016
@vkarpov15
Copy link
Collaborator

vkarpov15 commented Dec 3, 2016 via email

@codingleo
Copy link

I had this issue, as well. Thanks for help @VictorGerritsenQLVR!

@domarp-j
Copy link

domarp-j commented Apr 3, 2017

Is there a way to change the code to accommodate the arrow operator, rather than using function(next)?

@nickdotht
Copy link

@domarp-j Good question. I thought about it and tried to figure it out but I couldn't come up with anything easier than just switching to a regular callback function. Maybe someone else has done it tho. I'll keep an eye on this issue.

@vkarpov15
Copy link
Collaborator

No way to do it, fat arrows use lexical scope for context, no way around it.

@ZeeAzmat-punch
Copy link

had same issue, thanks for the help

@gdenn
Copy link

gdenn commented Jun 18, 2017

Hi, was stuck on the same problem with the es6 arrow function. Thank you very much!

@ghost
Copy link

ghost commented Feb 22, 2018

great thank u

@m00zi
Copy link

m00zi commented Apr 26, 2018

Thanks, works for me too.

@Automattic Automattic locked as resolved and limited conversation to collaborators Apr 27, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants