【发布时间】:2018-12-26 23:20:20
【问题描述】:
我正在尝试从 mysql 的条件重定向到 get 方法,但它不起作用。我该如何解决?
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_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
【问题讨论】:
-
请用minimal reproducible example扩展“它不起作用”。
-
根据 [stackoverflow.com/questions/38810114/… ,将重定向更改为
res.redirect(307, '/home'); -
@Uma 不工作。控制台显示:
throw err; // Rethrow non-MySQL errors ^Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
标签: javascript node.js