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

122
Views
node with express performs next() without running if statements

I have 2 if statements as middleware in my express app. The first one no problem, but before the second one it performs the next() function without running the second if statement.

  app.use((req: Request, res: Response, next: express.NextFunction) => {
    const email: string = req.body.email;
    const phoneNum: string = req.body.phone;
    console.log(phoneNum);
    if (phoneNum) {
        if (!phone(phoneNum).isValid) {
            res.json({
                message: "invalid phone format. Expecting format like: 8001234567",
            });
        } else {
            req.body.phone = phone(phoneNum).phoneNumber;
        }
    }
    if (email) {
        if (!EmailValidator.validate(email)) {
            res.json({
                message: "invalid email format. Expecting format like: name@domain.com",
            });
        }
    }
    next();
});

UPDATE: I tried return statements but the client doesn't receive a response. Updated code:

    app.use((req: Request, res: Response, next: express.NextFunction) => {
    const email: string = req.body.email;
    const phoneNum: string = req.body.phone;
    console.log(phoneNum);
    if (phoneNum) {
        if (!phone(phoneNum).isValid) {
            res.json({
                message: "invalid phone format. Expecting format like: 8001234567",
            });
        } else {
            req.body.phone = phone(phoneNum).phoneNumber;
            return;
        }
    } else {
        return;
    }
    if (email) {
        if (!EmailValidator.validate(email)) {
            res.json({
                message: "invalid email format. Expecting format like: name@domain.com",
            });
        } else {
            return;
        }
    } else {
        return;
    }
    next();
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's hard to fully tell the context of this middleware, but my instinct is that you want to return whenever you're sending a response to the client:

app.use((req: Request, res: Response, next: express.NextFunction) => {
  const email: string = req.body.email;
  const phoneNum: string = req.body.phone;
  console.log(phoneNum);
  if (phoneNum) {
    if (!phone(phoneNum).isValid) {
      res.json({
        message: "invalid phone format. Expecting format like: 8001234567",
      });
      return;
    } else {
      req.body.phone = phone(phoneNum).phoneNumber;
    }
  }
  if (email) {
    if (!EmailValidator.validate(email)) {
      res.json({
        message: "invalid email format. Expecting format like: name@domain.com",
      });
      return;
    }
  }
  next();
});
about 4 years ago · Juan Pablo Isaza Report
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!