【发布时间】:2021-07-25 19:37:25
【问题描述】:
我正在使用以下代码在 Lambda Edge 中设置会话标头作为响应。但我总是得到这个错误。
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'headers' of undefined",
"stack": [
"TypeError: Cannot read property 'headers' of undefined",
" at Runtime.exports.handler (/var/task/index.js:118:30)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
我正在使用的代码是这样的
exports.handler = function (event, config, callback) {
const request = event.Records[0].cf.request;
console.log(event.Records[0].cf);
const response = event.Records[0].cf.response;
const headers = response.headers;
getConfigCached(request, function (err, config) {
if (err) {
callback(err, null);
}
else if (request.uri !== "/code" && !redirectIfNotAuthenticated(config, request, callback)) {
callback(null, request);
}
else if (request.uri == "/code") {
console.log('INSIDE CODE');
let access_token = accessTokenCallback(request, callback);
console.log(access_token);
headers['session-token'] = [{ key: 'Session-Token', value: access_token }];
callback(null, response);
}
});
};
CloudFront 触发器设置为查看器请求
【问题讨论】:
-
显然
event.Records[0].cf.response是undefined。有什么问题? -
为什么未定义?我需要将触发器更改为 Viewer Response 吗?
标签: node.js amazon-cloudfront aws-lambda-edge