我正在尝试将cookie添加到另一个get请求中的响应对象(res)。但是,如果尝试以下操作,则会给我一个错误,在发送标头后无法设置标头。我假设通过调用“请求”已经发送了报头,如何使用来自单独的get请求的数据将cookie添加到响应对象?同步性质也不会让我将数据保存在get请求之外。我正在使用npm btw的请求模块,谢谢。
/* GET home page. */
router.get('/', function(req, res, next) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
var status = "Not currently logged in.";
if (req.cookies.token !== undefined) {
status = "Currently Logged in.";
}
if (req.cookies.email !== undefined) {
request('https://localhost:44338/api/customer/' + req.cookies.email
{json: true}, (err, response, body) => {
res.cookie('user', body[0].customerID, {maxAge: 9000000});
//console.log(body);
});
}
res.render('index', { title: 'Mighty Morphin Store', data: "", status: status});
});
答案 0 :(得分:0)
设置cookie后,您必须在回调函数中发送响应。
/* GET home page. */
router.get('/', function(req, res, next) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
var status = "Not currently logged in.";
if (req.cookies.token !== undefined) {
status = "Currently Logged in.";
}
if (req.cookies.email !== undefined) {
request('https://localhost:44338/api/customer/' + req.cookies.email
{json: true}, (err, response, body) => {
res.cookie('user', body[0].customerID, {maxAge: 9000000});
//console.log(body);
res.render('index', { title: 'Mighty Morphin Store', data: "", status: status});
});
}
});