Estou criando um sistema de registro de usuários no NodeJS e preciso verificar se o email digitado já está cadastrado, está tudo funcionando normalmente, se eu digito um email já existente ele me retorna o id do usuário que utiliza este email. Porém, na hora que eu vou escrever um retorno na tela com o res.send("");
a aplicação me retorna o erro do título.
(É uma API)
Meu código:
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;
}
Já realizei diversos testes, a consulta sql está ok, não está retornando erro, está retornando um result.length > 0
e quando chega no res.send("");
da erro.
Eu já terminei um sistema de login nesse mesmo formato e funcionou normalmente.