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

142
Visualizações
How to check for username and email in nestjs validation. The async function awaits for findbyusername and then never reaches the else condition

How to check for username and email in nestjs validation. The async function awaits for findbyusername and then never reaches the else condition.

I am trying to create one validation method to check if entered value in userNameorEmail field is username or email but looks like the function never reaches to the else statement. It just throws the error when I do then and catch. How can I make it work. Thanks for your help in advance.

  async validateUser(userNameorEmail: string, pass: string): Promise<any> {

    const resultByUsername = await this.usersService.findbyUsername(userNameorEmail);

    if (resultByUsername && resultByUsername.password === pass) {
      const { password, ...result } = resultByUsername;
      return result;

    } else {
      const resultByEmail = await this.usersService.findbyEmail(userNameorEmail);

      if (resultByEmail && resultByEmail.password === pass) {
        const { password, ...result } = resultByEmail;
        return result;

      }
    }

In my usermodel

async findbyUsername(uNameorEmail: string) {
    const user = await this.userModel
      .findOne({ username: uNameorEmail })
      .lean()
      .exec();
    if (!user) {
      throw new NotFoundException();
    }
    return user;
  }

  async findbyEmail(uNameorEmail: string) {
    const user = await this.userModel
      .findOne({ email: uNameorEmail })
      .lean()
      .exec();
    if (!user) {
      throw new NotFoundException();
    }
    return user;
  }
about 4 years ago · Juan Pablo Isaza
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