Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
Cannot read property 'emailAddressValidation' of undefined

I'm just learning node with MVC pattern.I made one Validation class to validate user data.

class UserValidation {
  constructor() {}

  emailAddressValidation(email) {
    console.log(email);
    const regularExpression =
      /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return regularExpression.test(email.toLowerCase());
  }

  passwordValidation(password) {
    const regularExpression =
      /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;
  }

  loginValidation(req, res, next) {
    const { email, password } = req.body;
    console.log(email);
    this.emailAddressValidation('addad');
    // try {
    //   if (
    //     this.emailAddressValidation(email) &&
    //     this.passwordValidation(password)
    //   ) {
    //     next();
    //   } else {
    //     throw new Error(
    //       'All fields are Required.email,password.Password should be alpha numeric.'
    //     );
    //   }
    // } catch (error) {
    //   res.status(400).json({ err: 1, message: error.message });
    // }
  }

  NewUserValidation(req, res, next) {
    const { name, email, password, address, skills } = req.body;

    try {
      if (
        name &&
        this.emailAddressValidation(email) &&
        this.passwordValidation(password) &&
        address.length > 0 &&
        skills.length > 0
      ) {
        next();
      } else {
        throw new Error(
          'All fields are required.Name,email,password,Address,Skills.Password should be alphanumeric.'
        );
      }
    } catch (error) {
      res.status(400).json({ err: 1, message: error.message });
    }
  }
}

export default new UserValidation();

Something like this and using it in my route like this

router.post('/login', UserValidation.loginValidation);

But I don't know why I got this error

TypeError: Cannot read property 'emailAddressValidation' of undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

this will refer to an instance of that class. You will have to initialize an object of UserValidation and call loginValidation on that object. Something like this:

const validator = UserValidation()
router.post('/login', validator.loginValidation);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda