在发布请求时无法重定向

时间:2016-03-29 07:59:39

标签: node.js express

我正在使用请求模块进行发布请求。在帖子请求之后,我希望重定向用户。但在我看来,我无法在这里进行重定向。

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
    });
}

1 个答案:

答案 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
   });
}
相关问题