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

124
Visualizações
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 Respostas
Responde à pergunta

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 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