【发布时间】:2021-06-18 07:51:27
【问题描述】:
Typescript 新手在这里。我正在使用带有类的打字稿来处理 AWS Lambda 函数。我在最后导出一个async 处理程序。当我从 AWS SAM CLI 调用我的函数时,我收到以下错误;
{"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
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();
这是一个非常基本的类,代表一个存储库。
foobar-repository.ts
export class FoobarRepository {
private readonly awesomeValue: string;
constructor() {
this.awesomeValue = 'John Doe';
}
async test(): Promise<string> {
return this.awesomeValue;
}
}
我几乎可以肯定这是因为我导出处理程序的方式以及aws-sam 在内部运行处理程序的方式。但我可能错了,它可能是我错过的打字稿。如果您需要更多信息,请告诉我,非常感谢您的帮助!
【问题讨论】:
-
async handler(event: APIGatewayProxyEventV2) : Promise<APIGatewayProxyResultV2>需要是箭头函数。您正在失去功能的上下文。写成handler = async (event: APIGatewayProxyEventV2) : Promise<APIGatewayProxyResultV2> => {..} -
@Townsheriff 感谢您的方法。我正在考虑更多的面向对象的实现方式,这在文章blog.10pines.com/2019/07/23/… 中进行了讨论。因此,使用
bind导出处理程序按预期工作。就像是;export const createCostLambda = new CreateCostLambda(); export const handler = createCostLambda.handler.bind(createCostLambda);对于我的要求,这是一个不好的方法吗?谢谢! -
我不知道你为什么想要这样的东西。您需要绑定,否则您将无法访问上下文。我只会使用一个函数 -
module.exports.handler = (...) => {...}。箭头函数将函数绑定到它自己的上下文,因此您可以在类中编写箭头“方法”,它将起作用typescriptlang.org/play?#code/… -
谢谢你。我采用了仅使用箭头函数的方法。
标签: node.js typescript aws-lambda es6-promise aws-sam