1

olá! tenho esse código em typescript:

import { InternalService } from './../services/InternalServices';
import { Request, Response } from "express";


class InternalController {
    internalService = new InternalService();

    constructor() {
    }

    async status(req: Request, res: Response): Promise<number> {
        return await this.internalService.status()
    }
}

export { InternalController };

e no arquivo ./../services/InternalServices tenho:

class InternalService {
    constructor() { }

    async status(): Promise<number> {
        return 200;
    }
}

export { InternalService }

está tudo visualmente perfeito mais quando eu rodo aparece a mensagem no termina:

(node:7281) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'internalService' of undefined
    at /workspace/Theryston/back-end/src/controllers/InternalController.ts:12:27
    at Generator.next (<anonymous>)
    at /workspace/Theryston/back-end/src/controllers/InternalController.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/workspace/Theryston/back-end/src/controllers/InternalController.ts:4:12)
    at status (/workspace/Theryston/back-end/src/controllers/InternalController.ts:19:16)
    at Layer.handle [as handle_request] (/workspace/Theryston/back-end/node_modules/express/lib/router/layer.js:95:5)
    at next (/workspace/Theryston/back-end/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/workspace/Theryston/back-end/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/workspace/Theryston/back-end/node_modules/express/lib/router/layer.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7281) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7281) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

alguém poderia me ajudar?

1
  • 1
    Esse erro não é do TypeScript, mas sim do JavaScript, já que está ocorrendo em runtime. 27/05/2021 às 20:39

1 Resposta 1

4

Você passou a função status como parâmetro de callback para outra função?

Me parece que ela não está reconhecendo o this, porque ela foi invocada sem o binding do objeto.

Se em algum ponto do seu código você tem algum código similar a isso:

const internalController = new InternalController();
middleware(internalController.status);

Tente refatora-lo dessa maneira:

const internalController = new InternalController();
middleware(() => internalController.status());
1

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .