我正在使用请求模块进行发布请求。在帖子请求之后,我希望重定向用户。但在我看来,我无法在这里进行重定向。
function callApi(req, res) {
request(options, function(err, response, body){
res.redirect('/'); //cannot read property redirect of undefined, can't set headers after they are sent
});
}
function callApi(req, res) {
request(options, function(err, response, body){
req.res.redirect('/'); // can't set headers after they are sent
});
}
答案 0 :(得分:1)
在内部回调中,您的res将引用请求回调的响应。这样做......
function callApi(request, response) {
request(options, function(req, res, body){
response.redirect('/'); //cannot read property redirect of undefined, can't set headers after they are sent
});
}
function callApi(request, response) {
request(options, function(req, res, body){
response.redirect('/'); // can't set headers after they are sent
});
}