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

223
Visualizações
next() isn't skipping over the rest of the middleware

I'm very much a newbie to the whole express routing logic (and node and js for that matter). But I have problem that I can't seem to trace, but I think I understand the context of it. Well now, that didn't make a lot of sense, here it goes anyway.

I'm trying to skip through middleware conditionally based on data in the req.query and it renders the intended form page fine except for the fact that I get 'Cannot send header twice ...' in the console from the previous middleware, which i thought i skipped.

From what I can gather next() should get me to the next middleware instantly and skip the rest of the block entirely (kind of like how a return kicks you out of a function). Is this not correct?

path: localhost:3000/JohnnyBoy?action=form

    router.get('/:name/', function(req, res, next) {

      if(req.query.action !== 'view') { next(); };

      console.log('Why am I seeing this in the console?');

      res.render('first', { 
        title: req.params.name 
        });
    });

    router.get('/:name/', function(req, res, next) {

        res.render('form', {  
                  title: req.params.name + ' This is a form page', 
                  formFields: fields.userPostFields()
                  });
    });
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to put return; after calling next(); only calling next() will not stop execution of the current function.

Replace

if(req.query.action !== 'view') { next(); };

with

if(req.query.action !== 'view') { next(); return;};

over 4 years ago · Santiago Trujillo 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