'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;
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source