node.js - Node js return can not set after headers they are sent -
i'm having problem node.js return-an error on console saying:
error: can not set after headers sent.
right here part of code returning error:
app.post ('/ register', function (req, res) { var = usuariocadastro req.body.usuario; var = emailcadastro req.body.email; var = telefonecadastro req.body.telefone; var = senhacadastro req.body.senha; console.log ("user:" + usuariocadastro); console.log ("email:" + emailcadastro); console.log ("telephone" + telefonecadastro); console.log ("password" + senhacadastro); pool.query('select * users email = ?',[emailcadastro], function(err, rows, fields) { if (err) throw err; (var in rows){ var emailbanco = rows[i].email; } if(emailcadastro === emailbanco){ console.log("email já foi cadastrado"); res.set('content-type', 'application/json'); res.send({ code: '303', mensagem:'this email signup' }); }else{ var sql = {usuario:usuariocadastro,email:emailcadastro,senha:senhacadastro}; pool.query('insert users set ?',[sql], function(err, rows, fields) { if (err) throw err; console.log("novo usuario cadastrado"); res.send({ code: '200', mensagem:'sucess, welcome trimtrim' }); });/*second query*/ }/* else */
});/* first query*/
i've been researching , saw, expression can send once. need send more times. do?
you can send headers once. aggregate messages , send them after loop.
connection.release (); var msg = ''; (var in rows) { if (emailcadastro === rows [i] .email) { console.log ("email database:" + rows [i] .email); if (msg.length>0) msg += '<br/>'; msg += 'e-mail registered: ' + rows[i].email; errodeform ++; } } // if (msg.length>0) res.json ({code: 303 message: msg});
Comments
Post a Comment