Can not set headers after they are sent - NodeJS

1

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

(It's 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 already performed several tests, the sql query is ok, it is not returning error, it is returning a result.length > 0 and when it arrives at res.send(""); of error.

I've already completed a login system in that same format and it worked fine.

    
asked by anonymous 02.09.2017 / 20:34

1 answer

2

You are making two asynchronous requests before sending res.send("ok") and within them, you send another res.send(...) . What happens is that once you give an answer to the request in res.send() , you can not send a new response.

You need to implement your functions so that only once is the response sent. One way you can 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");
    });
});
    
02.09.2017 / 20:53