Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda