Reputation: 51
Hi I have this error but I don't understand why the error appears, Please help me to solve it.
var fs = require("fs");
var port = 3000;
//Creating pool conection database
var mysql = require("mysql");
function ConnectDB()
{
this.result = "r";
this.conexion = mysql.createPool
({
host: "localhost",
user: "root",
password: "rometotalwar2",
database: "WebDatabase"
});
this.SignUpUser = function(nombre,apellidos,nick,pass,email,callback)
{
var querystring = "SELECT Nickname,Correo from Users WHERE Nickname='"+nick+"' and Correo='"+email+"';";
this.conexion.getConnection(function(err, connection)
{
if(err)
{
}
else
{
connection.query(querystring,function(err,result,fields)
{
if(err)
{
}
else
{
if(result.length != 0)
{
callback("NO");
}
else
{
var querystring = "INSERT INTO Users VALUES(null,'"+nombre+"','"+apellidos+"','"+nick+"','"+pass+"','"+email+"','Comun','Activo');";
connection.query(querystring,function(err,result,fields)
{
if(err)
{
}
});
}
}
});
connection.release();
}
});
callback();
}
}
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var server = express();
server.use(express.static(path.join(__dirname, 'public')));
server.use(bodyParser.urlencoded({extended: true}));
server.use(bodyParser.json());
server.listen(port,function()
{
console.log("Server listening at port: " + port);
});
server.post('/addUser',function(req,res,next)
{
var nombre = req.body.nombre;
var apellidos = req.body.apellido;
var nickname = req.body.nickname;
var email = req.body.email;
var pass = req.body.pass;
var conn = new ConnectDB()
conn.SignUpUser(nombre,apellidos,nickname,pass,email,function(err)
{
var head,body,status;
if(err == "NO")
{
head = "Error";
body = "User alredy exits";
status = 101;
}
else
{
head = "Ok";
body = "User Register";
status = 102;
}
res.send(body,head,status);
});
});
server.use(function(req, res, next)
{
var err = new Error('Not Found');
err.status = 404;
next(err);
});
module.exports = server;
This is the error that appears:
/home/francisco/Documentos/ServerNodejs/node_modules/mysql/lib/protocol/Parser.js:82
throw err;
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (/home/francisco/Documentos/ServerNodejs/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/home/francisco/Documentos/ServerNodejs/node_modules/express/lib/response.js:163:12)
at /home/francisco/Documentos/ServerNodejs/server.js:100:13
at Query._callback (/home/francisco/Documentos/ServerNodejs/server.js:40:29)
at Query.Sequence.end (/home/francisco/Documentos/ServerNodejs/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at Query._handleFinalResultPacket (/home/francisco/Documentos/ServerNodejs/node_modules/mysql/lib/protocol/sequences/Query.js:144:8)
at Query.EofPacket (/home/francisco/Documentos/ServerNodejs/node_modules/mysql/lib/protocol/sequences/Query.js:128:8)
at Protocol._parsePacket (/home/francisco/Documentos/ServerNodejs/node_modules/mysql/lib/protocol/Protocol.js:274:23)
at Parser.write (/home/francisco/Documentos/ServerNodejs/node_modules/mysql/lib/protocol/Parser.js:77:12)
Upvotes: 0
Views: 2446
Reputation: 12953
This error happens when you try to write something to the result after it was already sent- usually it means you that you are calling send()
twice on your result.
This happens because you are calling your callback twice - once in the if(result.length != 0)
segment, and again after all the if statements
Upvotes: 5