简体   繁体   English

TypeScript this 关键字上下文错误 - 未定义

[英]TypeScript this keyword context error - undefined

I have a aws lambda written in JS (TypeScript) which calls functions from different classes.我有一个用 JS(TypeScript)编写的 aws lambda,它调用来自不同类的函数。 I am getting undefined for the existingproducts variable even though it works fine when the REACT UI sends a call to the function.即使当 REACT UI 发送对该函数的调用时它工作正常,我也无法为现有产品变量定义 Below is my code using this keyword to reference the method of other class with current object in scope.下面是我的代码,使用这个关键字来引用范围内当前对象的其他类的方法。

Entry point for the lambda lambda 的入口点

export const handler = async (upload: FileUpload, context: Context) => {
     .....code .....
     const parser = new ExcelValidator(new LookupService(), new ProductService());
     const status = await parser.performExistingUpcValidation(products as Product[], upload, workbook);
     return status

PerformExisitingUPCValidation执行现有 UPC 验证

export class ExcelValidator {

    constructor(public lookupService: LookupService, public productService: ProductService) {

    }

    async performExistingUpcValidation(products: Product[], upload: FileUpload, workbook?: Workbook): Promise<FileStatus> {
       ...code...
       const existingProducts: any[] = await this.productService.getExistingProductsByUpcOrProductCode(productUpcs, productCodes);
       console.log("This is the exisitingProduct", existingProducts)
}

ProductServiceClass产品服务类

export class ProductService {


    constructor() {
    }

    @Query(()=>[Product])
    async getExistingProductsByUpcOrProductCode(@Arg("upcs", ()=> [String]) upcs: string[], @Arg("productCodes", ()=> [String]) productCodes: string[]): Promise<Product[]> {
        console.log("I came here")
        let query = `SELECT * from table
            in (${upcs.join(",")})`;
        if(productCodes.length){
            query += ` OR "productCode" in ('${productCodes.join("','")}')`;
        }
        const results = await pool.snowflake?.execute(query);
        return results as Product[];
    }

After all the execution I am able to see在所有执行之后,我能够看到

This is the exisitingProduct undefined

which means my execution does not reach the ProductServiceClass.这意味着我的执行没有到达 ProductServiceClass。 Can someone point me to what is wrong or missing?有人可以指出我有什么问题或遗漏吗? Also any documentation/reference to read more will help alot.此外,任何阅读更多的文档/参考都会有很大帮助。

My suggestion for you is review the item below:我对您的建议是查看以下项目:

const results = await pool.snowflake?.execute(query);

Are you confident that snowflake is not null, because you are accepting to be undefined with help of the question mark.您是否确信雪花不为空,因为您在问号的帮助下接受未定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM