Javascript 如何修复节点js中的集合头?
我试图从mysql的条件重定向到get方法,但它不起作用。我该怎么解决呢Javascript 如何修复节点js中的集合头?,javascript,node.js,Javascript,Node.js,我试图从mysql的条件重定向到get方法,但它不起作用。我该怎么解决呢 app.post('/responses/',(req,res)=>{ var {text, context ={} } = req.body; var params = { input: {text}, workspace_id: '07', context }` `assistant.message(params,(err,respo
app.post('/responses/',(req,res)=>{
var {text, context ={} } = req.body;
var params = {
input: {text},
workspace_id: '07',
context
}`
`assistant.message(params,(err,response) => {
if(err){
res.status(500).json(err);
}
else {
res.json(response);
console.log(JSON.stringify(response, null, 2));
}
if(response.context.rfc){
var RFC = response.context.rfc;
connection.connect(function(err) {
if (err) throw err;
connection.query(`SELECT * FROM test where RFC = '${RFC}'`, function (err, result, fields) {
if(result.length){
console.log("login");
}
else {
console.log('no login');
res.redirect('/home'); //Her not redirect.
}
});
});
}
});
});
因为显示此错误:
hrow err;//Rethrow non-MySQL errors^Error[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头文件
错误消息表示您正在尝试在发送响应后设置头文件。从示例代码看,您的API调用(无论assistant.message
是什么)似乎正在返回RFC。然后尝试重定向到另一个页面,但您已经调用了res.json
,它开始向浏览器发送响应
您需要稍微重构控制器,以便只调用
res.redirect
或res.json
,而不是两者都调用。请使用.per[,将重定向更改为res.redirect(307,/home');
@Uma不工作。控制台显示:throw Error;//Rethrow non-MySQL errors^错误[ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置头文件
Nelson我如何使用其他res?发送后无法设置任何头文件或再次发送res。您需要重新排列if/else语句,以便在不重定向或返回错误的情况下,只能使用res.json
响应。