C:\Users\jsj10\node_modules\mysql\lib\protocol\Parser.js:80
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (C:\Users\jsj10\node_modules\express\lib\response.js:767:10)
at ServerResponse.append (C:\Users\jsj10\node_modules\express\lib\response.js:728:15)
at ServerResponse.res.cookie (C:\Users\jsj10\node_modules\express\lib\response.js:853:8)
at ServerResponse.clearCookie (C:\Users\jsj10\node_modules\express\lib\response.js:800:15)
at Query.<anonymous> (C:\Users\jsj10\Desktop\TravelA\server.js:177:18)
at Query.<anonymous> (C:\Users\jsj10\node_modules\mysql\lib\Connection.js:502:10)
at Query._callback (C:\Users\jsj10\node_modules\mysql\lib\Connection.js:468:16)
at Query.Sequence.end (C:\Users\jsj10\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
Program node server.js exited with code 1
这是我的错误。 尽管我看了很多问题,但我找不到问题所在。请帮我什么问题。我认为我所有的代码都没有错。告诉我应该在哪里修理。 这是我的代码。
app.post('/writing_server',function(req,res){
var title = req.body.writing_title;
var author = req.body.author;
var contents = req.body.contents;
var user_id = req.body.user_id;
var sql = "insert into "+before_page+"(title,author,content,user_id) values(?,?,?,?)";
if(user_info.id != 'admin' && author.toLowerCase() === 'admin'){
write_message = "You can't use admin NickName!";
res.redirect('/write?before='+before_page);
}
else {
conn.query(sql,[title,author,contents,user_info.id],function(err,rows,fields){
if(err) console.log(err);
else {
res.redirect('/menu/'+before_page);
res.clearCookie('before_page');
}
});
}
})
答案 0 :(得分:1)
在Express中,路由方法(response
,res
等)的回调的app.get()
(或app.post()
)参数具有将数据发送回的方法到客户端(例如res.redirect()
)和在调用res.redirect()
之类的方法之前设置标头数据的方法。
在您的代码中,您正在尝试执行以下操作:
res.redirect('/menu/'+before_page);
res.clearCookie('before_page');
但是应该相反:
res.clearCookie('before_page');
res.redirect('/menu/'+before_page);
res.clearCookie()
方法为res.redirect()
设置标题数据以供使用,因此必须按此顺序调用这些方法。
答案 1 :(得分:0)
按以下顺序放置这两行
SELECT Distinct
table.Cluster1,
table.Cluster2,
ConcatRelated("Reason", "table", "Cluster1=""" & [Cluster1] & """ and Cluster2=""" & [Cluster2] & """") AS Reasons
FROM table;
您可以使用
检查操作是否将标头发送到客户端 res.clearCookie('before_page');
res.redirect('/menu/'+before_page);
https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections