简体   繁体   English

AWS Lambda 与 typescript 获取无法读取异步处理程序内部未定义的属性

[英]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.

相关问题 AWS Lambda:“无法读取未定义的属性‘0’” - AWS Lambda: "Cannot read property '0' of undefined" AWS Lambda TypeError:无法读取未定义的属性“ toString” - AWS lambda TypeError: Cannot read property 'toString' of undefined AWS Lambda 函数:无法读取未定义的属性“logs”“numberOfPages”“stream”。 包问题还是 AWS Lambda 问题? - AWS Lambda Function: Cannot read property 'logs' 'numberOfPages' 'stream' of undefined. Package issue or AWS Lambda issue? v-on 处理程序中的错误(Promise/async):“TypeError:无法读取未定义的属性“数据””//未定义 - Error in v-on handler (Promise/async): “TypeError: Cannot read property 'data' of undefined” // undefined 异步 - 类型错误:无法读取未定义的属性“应用” - async - TypeError: Cannot read property 'apply' of undefined AWS Lambda 上的 Apollo 网关无法读取未定义的属性“内容类型” - Apollo Gateway on AWS Lambda Cannot read property 'content-type' of undefined 无法读取未定义的属性“insertOnMatch” - TypeScript - Cannot read property 'insertOnMatch' of undefined - TypeScript 无法读取未定义的属性“userid”:TypeScript - Cannot read property 'userid' of undefined : TypeScript 运行lambda“ errorMessage”时收到以下错误:“无法读取未定义的属性&#39;requestParameters&#39;”, - Getting the following error when running lambda “errorMessage”: “Cannot read property 'requestParameters' of undefined”, 获取类型错误:无法读取未定义的属性查找 - Getting a typeerror: cannot read property find of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM