2016-03-29 7 views
0

投稿リクエストを行うリクエストモジュールを使用しています。投稿リクエストの後、私はユーザーをリダイレクトしたいと思います。しかし、私はここでリダイレクトすることができないと私には思われます。ポストリクエストを行うときにリダイレクトできません

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

ステータスコードres.statuscode = 301;を作成しようとしましたか? –

+0

@DayanMorenoLeonそれはどういう意味ですか?私は 'req.redirect(301、 '/')'を試しても、同じ結果を返します。 – stackdisplay

+0

res.statusCode = '301'; res.redirect( '/'); res.end( '');あなたが使用しているエクスプレスバージョンに応じて、より少ないステップで動作します。あなたのコードでreq.resを使用することに注意してください。ただresです。 –

答えて

1

コールバックでは、リクエストはコールバックの応答を参照します。これを行う...

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

私は誤って更新するので、このトピックで自分のコードを更新しました。それは '要求(options、function(error、response、body)'でなければなりません。だから、これは問題ではないと思います – stackdisplay

関連する問題