您好我有这个错误,但我不明白为什么会出现错误,请帮我解决。
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;
这是出现的错误:
/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)
答案 0 :(得分:2)
当您尝试在结果发送后向结果写入内容时会发生此错误 - 通常这意味着您在结果上调用send()
两次。
这是因为您在两次调用回调 - 一次在if(result.length != 0)
段,再次在所有if语句之后