Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 类型脚本为getting的AWS Lambda无法读取异步处理程序中未定义的属性_Node.js_Typescript_Aws Lambda_Es6 Promise_Aws Sam - Fatal编程技术网

Node.js 类型脚本为getting的AWS Lambda无法读取异步处理程序中未定义的属性

Node.js 类型脚本为getting的AWS Lambda无法读取异步处理程序中未定义的属性,node.js,typescript,aws-lambda,es6-promise,aws-sam,Node.js,Typescript,Aws Lambda,Es6 Promise,Aws Sam,这里是打字新手。我正在使用带有类的typescript处理AWS Lambda函数。我在最后导出一个async处理程序。当我从AWS SAM CLI调用我的函数时,我得到的错误是 {“errorType”:“TypeError”,“errorMessage”:“无法读取未定义的属性'test'”,“stack”:[“TypeError:无法读取未定义的属性'test'”,“at Runtime.handler(/var/task/src/lambda/create cost lambda.js:

这里是打字新手。我正在使用带有类的typescript处理AWS Lambda函数。我在最后导出一个
async
处理程序。当我从AWS SAM CLI调用我的函数时,我得到的错误是

{“errorType”:“TypeError”,“errorMessage”:“无法读取未定义的属性'test'”,“stack”:[“TypeError:无法读取未定义的属性'test'”,“at Runtime.handler(/var/task/src/lambda/create cost lambda.js:12:56)”,“at Runtime.handleOnce(/var/Runtime/Runtime/Runtime.js:66:25)”]

创建成本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();
export class FoobarRepository {
    private readonly awesomeValue: string;

    constructor() {
        this.awesomeValue = 'John Doe';
    }

    async test(): Promise<string> {
        return this.awesomeValue;
    }
}
类CreateCostLambda{
私有只读foobarepository:foobarepository;
构造函数(){
this.foobarepository=新的foobarepository();
}
异步处理程序(事件:APIGatewayProxyEventV2):承诺{
const result=等待这个.foobarepository.test();
控制台日志(结果);
返回{
正文:JSON.stringify(结果),
状态代码:200,
};
}
}
export const{handler}=new CreateCostLambda();
下面是一个表示存储库的非常基本的类

foobar repository.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();
export class FoobarRepository {
    private readonly awesomeValue: string;

    constructor() {
        this.awesomeValue = 'John Doe';
    }

    async test(): Promise<string> {
        return this.awesomeValue;
    }
}
export-class-foobarepository{
私有只读awesomeValue:string;
构造函数(){
this.awesomeValue='John Doe';
}
异步测试():承诺{
返回此.awesomeValue;
}
}

我几乎可以肯定这是因为我导出处理程序的方式以及
aws sam
如何在内部运行处理程序。但我可能错了,可能是我遗漏了打字稿。如果您需要更多信息,请告诉我,并感谢您的帮助

简短的版本是,如果您从类中传递函数,它将丢失对
的引用

我将通过以下方式解决此问题:

const createCostLambda=new createCostLambda();
export const handler=createCostLambda.handler.bind(createCostLambda);
你也可以问自己,这需要成为一门课吗?答案是:可能不是。在你的样品中没有从中得到什么

const foobarepository=new foobarepository();
导出异步函数处理程序(事件:APIGatewayProxyEventV2):承诺{
const result=wait foobarrespository.test();
控制台日志(结果);
返回{
正文:JSON.stringify(结果),
状态代码:200,
};
}

更少的行,没有不必要的状态。Javascript不是Java=)

异步处理程序(事件:APIGatewayProxyEventV2):Promise
需要是一个箭头函数。您正在失去功能的上下文。像
handler=async(event:APIGatewayProxyEventV2)那样编写:Promise=>{..}
@Townsheriff谢谢你的方法。我考虑的更多的是一种面向对象的实现方式,这将在本文中讨论。因此,使用
bind
导出处理程序按预期工作。有点像<代码>导出常量createCostLambda=新建createCostLambda();export const handler=createCostLambda.handler.bind(createCostLambda)对于我的问题,这是一种糟糕的方法吗?谢谢我不知道你为什么想要那样的东西。您需要绑定,否则无法访问上下文。我将使用一个函数-
module.exports.handler=(…)=>{…}
。Arrow函数将函数绑定到它自身的上下文,这样您就可以在类中编写Arrow“方法”,它将正常工作。我使用的是箭头函数。是的,你是对的。我想如果我使用一个类中的handler方法,它会更好地用于测试目的(我是javaguy:)。因此,我采用了导出函数的方法。再次感谢