Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
Sequelize instance method not working?

I'm trying to get access to the password stored in the user instance using keyword this and it doesn't work?

Here is the code when I'm calling it.

Users.find({where: {$or: [{ email: email} ,{ username: username }]}})
  .then(user => {
    if (!user) return res.status(401).send(new errors.Unauthorized('Email/Username or Password Not Found!'));

    user.authenticate(password, (err, match) => {
      if (err) return res.status(401).send(err);
      res.send(user);
    });
  })
  .catch(err => res.send(err));

Here is the code for my instance method.

    instanceMethods :{
      authenticate: (password, cb) => {
        return bcrypt.compare(password, this.password, (err, match) => {
          if (err) return cb(new errors.Unauthorized());
          return cb(null, match);
        });
      }
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You use arrow function, which doesn't let this to be bound via call() or apply(). See MDN:

Since this is not bound in arrow functions, the methods call() or apply() can only pass in parameters. this is ignored.

And further,

As stated previously, arrow function expressions are best suited for non-method functions...

See this github issue as well https://github.com/sequelize/sequelize/issues/5858

There is no way to override the scope of arrow functions - that's one of their main features. So there's nothing we can do here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!