[英]AWS Lambda with typescript getting Cannot read property of undefined inside async handler
Typescript newbie here. Typescript 新手在这里。 I am working on an AWS Lambda function by using typescript with classes.我正在使用带有类的 typescript 来研究 AWS Lambda function。 I am exporting an async
handler at the end.我在最后导出一个async
处理程序。 When I invoke my function from AWS SAM CLI then I am getting error of;当我从 AWS SAM CLI 调用我的 function 时,我收到以下错误;
{"errorType":"TypeError","errorMessage":"Cannot read property 'test' of undefined","stack":["TypeError: Cannot read property 'test' of undefined"," at Runtime.handler (/var/task/src/lambda/create-cost-lambda.js:12:56)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]} {"errorType":"TypeError","errorMessage":"Cannot read property 'test' of undefined","stack":["TypeError: Cannot read property 'test' of undefined"," at Runtime.handler (/var /task/src/lambda/create-cost-lambda.js:12:56)"," 在 Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
create-cost-lambda.ts创建成本 lambda.ts
class CreateCostLambda {
private readonly foobarRepository: FoobarRepository;
constructor() {
this.foobarRepository = new FoobarRepository();
}
async handler(event: APIGatewayProxyEventV2) : Promise<APIGatewayProxyResultV2> {
const result = await this.foobarRepository.test();
console.log(result);
return {
body: JSON.stringify(result),
statusCode: 200,
};
}
}
export const { handler } = new CreateCostLambda();
Here is a very basic class represents a repository.这是一个非常基本的 class 代表一个存储库。
foobar-repository.ts foobar-repository.ts
export class FoobarRepository {
private readonly awesomeValue: string;
constructor() {
this.awesomeValue = 'John Doe';
}
async test(): Promise<string> {
return this.awesomeValue;
}
}
I am almost sure it is because of the way I am exporting the handler and how aws-sam
internally runs the handler.我几乎可以肯定这是因为我导出处理程序的方式以及aws-sam
如何在内部运行处理程序。 But I might be wrong and it can be typescript thing that I am missing.但我可能错了,它可能是我缺少的 typescript 东西。 Please let me know if you need more information and thanks a lot for the help!如果您需要更多信息,请告诉我,非常感谢您的帮助!
The short version is if you pass a function from an class, it loses it's reference to this
.简短的版本是,如果您从 class 传递 function,它将失去对this
的引用。
I would solve this as follows:我将按如下方式解决此问题:
const createCostLambda = new CreateCostLambda();
export const handler = createCostLambda.handler.bind(createCostLambda);
You can also ask yourself, does this need to be a class?你也可以问问自己,这需要是class吗? The answer is: probably not.答案是:可能不会。 There's nothing gained from this in your sample.在您的样本中没有任何收获。
const foobarRepository = new FoobarRepository();
export async function handler(event: APIGatewayProxyEventV2) : Promise<APIGatewayProxyResultV2> {
const result = await foobarRepository.test();
console.log(result);
return {
body: JSON.stringify(result),
statusCode: 200,
};
}
Fewer lines, no unneeded state.更少的行,没有不需要的 state。 Javascript is not Java =) Javascript 不是 Java =)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.