The property this is returning Undefined in typescript

Asked

Viewed 37 times

1

hello! I have this code 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 };

and in the archive ./../services/Internalservices have:

class InternalService {
    constructor() { }

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

export { InternalService }

everything is visually perfect more when I rotate appears the message at the end:

(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.

someone could help me?

  • 1

    This error is not from Typescript, but from Javascript, since it is occurring in Runtime.

1 answer

3


You passed the function status as callback parameter for another function?

It seems to me she’s not recognizing the this, because she was invoked without the Binding of the object.

If at any point in your code you have some code similar to this:

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

Try to refactor it that way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.