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

136
Views
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
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!