Can’t set headers after they are sent - Nodejs

Asked

Viewed 377 times

1

I am creating a user registration system in Nodejs and I need to check if the email entered is already registered, everything is working normally, if I type an existing email it returns the id of the user that uses this email. However, at the time I will write a return on the screen with the res.send(""); the application returns me the title error.

(Is an API)

My code:

app.post('/account/new/validate', function(req, res){
    if(typeof req.body.emailRegistro !== 'undefined' && typeof req.body.senhaRegistro !== 'undefined' && typeof req.body.senhaConfirmarRegistro !== 'undefined' && typeof req.body.nomeRegistro !== 'undefined' && typeof req.body.cpfRegistro !== 'undefined' && typeof req.body.nascimentoRegistro !== 'undefined' && typeof req.body.cepRegistro !== 'undefined' && typeof req.body.estadoRegistro !== 'undefined' && typeof req.body.cidadeRegistro !== 'undefined' && typeof req.body.bairroRegistro !== 'undefined' && typeof req.body.ruaRegistro !== 'undefined' && typeof req.body.numeroRegistro !== 'undefined'){
        if(req.body.emailRegistro !== "" && req.body.senhaRegistro !== "" && req.body.senhaConfirmarRegistro !== "" && req.body.nomeRegistro !== "" && req.body.cpfRegistro !== "" && req.body.nascimentoRegistro !== "" && req.body.cepRegistro !== "" && req.body.estadoRegistro !== "" && req.body.cidadeRegistro !== "" && req.body.bairroRegistro !== "" && req.body.ruaRegistro !== "" && req.body.numeroRegistro !== ""){
            if(req.body.senhaRegistro === req.body.senhaConfirmarRegistro){
                var sql = app.config.dbcnx();
                var accountModel = app.app.models.accountModel;
                //Verificando se o email e o cpf estão em uso
                accountModel.verEmailExiste(sql, req.body.emailRegistro, function(error, result){
                    if(result.length > 0){ res.send("[EmailEmUso]"); }
                });
                accountModel.verCpfExiste(sql, req.body.cpfRegistro, function(error, result){
                    if(result.length > 0){ res.send("CpfEmUso"); }
                });
                res.send("ok");
            }else{ res.send("[SenhasDiferentes]"); }
        }else{ res.send("[CamposEmBranco]"); }
    }else{ res.send("[CamposIndefinidos]"); }   
});

Model:

module.exports = function(){
    this.verEmailExiste = function(sql, email, callback){
        sql.query("SELECT idUser FROM tblUsers WHERE emailUser='" + email + "';", callback);
    }
    this.verCpfExiste = function(sql, cpf, callback){
        sql.query("SELECT idUser FROM tblUsers WHERE cpfUser='" + cpf + "';", callback);
    }
    return this;
}

I have performed several tests, the sql query is ok, is not returning error, is returning a result.length > 0 and when it comes to res.send(""); of error.

I have already finished a login system in this same format and worked normally.

1 answer

2


You are making two asynchronous requests before sending one res.send("ok") and within them, you send another res.send(...). What happens is that after you give an answer to the request on res.send(), you cannot send a new reply.

You need to implement your functions so that you only send the reply once. One of the ways you do this is:

accountModel.verEmailExiste(sql, req.body.emailRegistro, function(error, result){
    // Lembre-se de dar um return ao final da sua condição
    // para que não continue a execução do código e envie
    // um outro res.send()
    if(result.length > 0){ res.send("[EmailEmUso]"); return; }

    accountModel.verCpfExiste(sql, req.body.cpfRegistro, function(error, result){
        if(result.length > 0){ res.send("CpfEmUso"); return; }
        // Tudo ok. Não tem ninguém com mesmo Email, nem CPF.
        // Agora você pode enviar uma resposta de ok
        res.send("ok");
    });
});
  • It worked, I thought when I sent a replay the code stopped automatically, without needing a Return.

  • I thought so too, but after checking with a console.log, I realized the shit hehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.