gpt4 book ai didi

javascript - express 发送后无法设置标题

转载 作者:搜寻专家 更新时间:2023-10-31 23:02:05 25 4
gpt4 key购买 nike

我很喜欢这里的常见问题。但我无法从任何地方得到解决方案。这是我的代码:如果行不为空,则呈现代码页,否则执行另一个操作。

app.get('/send',function(req,res){
var code=req.query['c']; // -- get request from input
connection.query("use mynum");
var strQuery = "select * from table WHERE code='"+code+"' LIMIT 1";
connection.query( strQuery, function(err, rows){
if(err) {
throw err;
}else{
if(rows.length==1){
res.render('pages/code', {code : rows[0].code});
connection.end();
res.end();
}else {
// here is some actions
}
}
});
res.end();
});

堆栈跟踪:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.header (C:\wamp\www\vin_number\node_modules\express\lib\re
sponse.js:666:10)
at ServerResponse.res.contentType.res.type (C:\wamp\www\vin_number\node_modu
les\express\lib\response.js:532:15)
at ServerResponse.send (C:\wamp\www\vin_number\node_modules\express\lib\resp
onse.js:121:14)
at fn (C:\wamp\www\vin_number\node_modules\express\lib\response.js:900:10)
at View.exports.renderFile [as engine] (C:\wamp\www\vin_number\node_modules\
ejs\lib\ejs.js:323:3)
at View.render (C:\wamp\www\vin_number\node_modules\express\lib\view.js:93:8
)
at EventEmitter.app.render (C:\wamp\www\vin_number\node_modules\express\lib\
application.js:530:10)
at ServerResponse.res.render (C:\wamp\www\vin_number\node_modules\express\li
b\response.js:904:7)
at Query._callback (C:\wamp\www\vin_number\server.js:102:6)

最佳答案

您通过 res.end() 发送了两次响应。摆脱第二个,你应该没事。此外,在 res.render() 之后调用 res.end() 是多余的,因为 res.render() 会自动以渲染结束响应默认结果。

关于javascript - express 发送后无法设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27658997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com