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

413
Visualizações
I get Error: Can't set headers after they are sent

I am new in node js and I stack with this error longtime. Can someone help me to find a solution! here is the code taht I have written !

router.get('/list',function (req, res, next)
{
   res.render('listaccounts.ejs',{mybalance:mybalance});
 });
 //accounts creation
router.post('/', function(req, res, next) {
var passphrase = req.body.passphrase;
var mybalance=web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]));
var accountAddress=web3_.personal.newAccount("passphrase",function(error,result){

   if(!error){
    console.log(result);
             }
   else {
    res.render('error.ejs');
       }
    });


res.redirect('/accounts/list');

  });
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem lays in

    if(!error){
    console.log(result);
             }
   else {
    res.render('error.ejs');
       }
    });


res.redirect('/accounts/list');

When it catches else statement, it does render the page, then you call page redirect, but headers were sent during res.render. In short words, you can't render and redirect at the same time

over 4 years ago · Santiago Trujillo Relatório

0

Just as Jakub said. Also you maty have an async execution problem (you don't know what will execute first, the redirect or the render). I think you could simply:

var accountAddress=web3_.personal.newAccount("passphrase",function(error,result){

   if(!error){
    console.log(result);
    res.redirect('/accounts/list');
   }
   else {
    res.render('error.ejs');
   }
 });
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