gpt4 book ai didi

javascript - Express js "res.append"错误 : Can't set headers after they are sent

转载 作者:太空宇宙 更新时间:2023-11-04 02:25:16 25 4
gpt4 key购买 nike

在我的 Express 项目中,我调用“res.append”来在同一请求中设置多个 cookie。但我收到这样的错误“错误:发送后无法设置 header 。”。有人能找出问题并给出解决方案或告诉其他更好的方法吗?

我的代码如下:

var setCookie = function(res, key, value, hrs, callback) {
if (typeof value === 'number'){value = value.toString(); }
if (typeof value === 'object'){value = JSON.stringify(value); }
var c;
if (hrs === 0) {
c = cookie.serialize(key, value, {httpOnly: true, path: '/', signed: true});
}
else {
c = cookie.serialize(key, value, {
maxAge: 3600 * hrs,
httpOnly: true,
path: '/',
signed: true
});
}
res.append('Set-Cookie', c);
callback(1);
};

var setLoginCookies = function(req, res, uaid, uuid, locale, trusted, callback) {
//sid
console.log('setLoginCookies:sid');
setCookie(res, 'sid', req.sessionID, 168, function () {
//uuid
console.log('setLoginCookies:uuid');
setCookie(res, 'uid', uuid, 168, function () {
//locale
console.log('setLoginCookies:locale');
setCookie(res, 'locale', locale, 0, function () {
callback(1);
});
});
});
};

错误:

GET / 200 748.366 ms - 5209
setLoginCookies:sid
D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\lib\utils.js:97
process.nextTick(function() { throw err; });
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (D:\mongodb\Project\node_modules\express\lib\response.js:700:10)
at ServerResponse.append (D:\mongodb\Project\node_modules\express\lib\response.js:670:15)
at setCookie (D:\mongodb\Project\com_modules\ran-doratupaala\app\libs\libCom.js:88:9)
at Object.setLoginCookies (D:\mongodb\Project\com_modules\ran-doratupaala\app\libs\libCom.js:123:5)
at D:\mongodb\Project\com_modules\ran-doratupaala\index.js:101:28
at D:\mongodb\Project\node_modules\express-session\session\store.js:43:5
at D:\mongodb\Project\node_modules\connect-mongo\lib\connect-mongo.js:437:9
at handleCallback (D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\lib\utils.js:95:12)
at D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\lib\collection.js:763:5
at D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:795:13
at Callbacks.emit (D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:94:3)
at null.messageHandler (D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:235:23)
at Socket.<anonymous> (D:\mongodb\Project\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:259:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
16 Jun 19:58:05 - [nodemon] app crashed - waiting for file changes before starting...

最佳答案

显示您如何调用 setLoginCookies 函数的上下文?

看起来有些东西,也许 cookie.serialize 可能是异步的,导致 javascript 在 setLoginCookies 之后继续执行而不是等待它完成。

另外,为什么不使用链接模式

cookiez(res)
.set('sid', req.sessionID, 168)
.set('uid', uuid, 168)
.set('locale', locale, 0);

关于javascript - Express js "res.append"错误 : Can't set headers after they are sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30867801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com